下面我们来一起看一下自定义cell的第二种复用方式,通过注册复用,获取数据参考上一篇微博:
http://blog.youkuaiyun.com/lee727n/article/details/72584599
注册复用自定义cell
viewdidload中注册
- (void)viewDidLoad {
[super viewDidLoad];
//向 tableView 注册 cell 的样式 如果是 纯代码的自定Cell 使用该方法
// [self.tableView registerClass:[ListCell class] forCellReuseIdentifier:@"listcell"];
//向 tableView 注册 cell 的样式 这里需要tableView 根据 xib 帮我创建cell 对象
UINib *listnib = [UINib nibWithNibName:@"ListCell" bundle:nil];
[self.tableView registerNib:listnib forCellReuseIdentifier:@"listcell"];
//通过属性 设置行高
self.tableView.rowHeight = 80;
}
实现三问一答
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allLists.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//复用方式二
ListCell *listCell = [tableView dequeueReusableCellWithIdentifier:@"listcell" forIndexPath:indexPath];
NewsList *newsList = self.allLists[indexPath.row];
listCell.newsTitleLabel.text = newsList.title;
listCell.newsCommentCountLabel.text = [NSString stringWithFormat:@"%ld", newsList.commentCount];
listCell.newsImageView.image = [UIImage imageNamed:newsList.newsImage];
return listCell;
}