如何遍历 tabelview 然后给其每一行控件属性赋值

iOS 码拜 9年前 (2015-05-01) 872次浏览 0个评论
 

如何遍历 tabelview 然后给其每一行控件属性赋值
在页面加载初始化的时候已经用 – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法初始化了数据

但是后面接口写好后,我要把从接口中获取的数值重新赋值给cell里的每一个控件 这个怎么循环然后重新赋值 麻烦说详细点最好是有代码的 100 分献上

如何遍历 tabelview 然后给其每一行控件属性赋值
100分
1.先定义一个存储数据的列表 

@property (nonatomic,strong) NSArray *list;

2. 从服务器请求得到返回的数据,赋给 list

self.list  = getDataFromServer();

3. 在tableview的datasource代理方法numerofRowsInSection中返回list的count

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.list.count;
}

4. tableView的datasource代理方法cellForRowAtIndexPath:即可在这个方法中将数据绑定到cell相应的控件上

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          //////根据indexPath.row 得到数据源上对应的要显示的数据
         id data = self.list[indexPath.row];
        //////创建cell的实例,我就忽略不写了,主要写赋值的过程
        ////1.如左边的书封面使用的UIimageView,从数据源中读取图片地址,我这里假设为coverImage为UIImageView的实例
       //// data[@"coverImage"] 从数据源中取key为coverImage的value,它对应的值是image的路径。如果是网络图片,建议开启一个异步线程加载
       cell.coverImage.image = [UIImage imageWithNamed:data[@"coverImage"]]; 
       cell.bookName.text = data[@"bookName"]; 
      ////.......其它的赋值类似,忽略不写了。

     return cell;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明如何遍历 tabelview 然后给其每一行控件属性赋值
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!