自定义cell的复用方式1

来源:互联网 发布:增城楼盘成交数据 编辑:程序博客网 时间:2024/05/29 04:10

首先我们导入plist文件,引入数据管理类。快速获取演示需要使用的数据模型。具体操作参考前一篇微博:
http://blog.csdn.net/lee727n/article/details/72584379
首先通过类方法获取数据模型的数据:

@interface NewsListTableViewController ()@property(nonatomic,strong)NSArray *allLists;@end@implementation NewsListTableViewController-(NSArray *)allLists {    if(_allLists == nil) {        _allLists = [DataManager allNewsLists];    }    return _allLists;}

实现三问一答,使用第一种非注册复用方式。注意通过xib自定义的cell需要load nib

- (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"];    if (!listCell) {/*        在当前应用程序中 找到 ListCell 这个xib 将 xib文件中所有的视图对象创建出来, 返回的是一个视图对象的数组 [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:nil options:nil] */        listCell = [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:nil options:nil].firstObject;    }    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;}

效果如下:
这里写图片描述

原创粉丝点击