下面是一些简单的表视图的应用案例:
1 微博 :练习使用自定义单元格,主要是计算好每个单元格的高度,以及单元格内每个控件的frame,因为有些是会员,有的有插图,有的没插图,所以计算略显复杂
#pragma mark - 复写get方法
-(NSArray *)weibos{
if (_weibos == nil) {
//获取plist文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
//将数据放进数组
self.weibos = [NSArray arrayWithContentsOfFile:path];
//定义一个可变数组 存放模型
NSMutableArray *models = [NSMutableArray array];
//字典转模型
for (NSDictionary *dict in self.weibos) {
WXWeibo *model = [WXWeibo weiboWithDict:dict];
[models addObject:model];
}
_weibos = models;
}
return _weibos;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.frame = CGRectMake(0, 64, 375, 603);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.weibos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 1 获取模型对象
WXWeibo *weibo = self.weibos[indexPath.row];
// 2 创建单元格
static NSString *identifier = @"weibo_cell";
WXWeiboCell *cell = [[WXWeiboCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
// 3 给单元格赋值
cell.weibo = weibo;
// 4 返回单元格
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
WXWeibo *weibo = self.weibos[indexPath.row];
NSString *text = weibo.text;
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(350, 1000)];
//如果有图片 加上图片的高度
if (weibo.picture) {
return size.height + 40 + 35 + 100;
}else{
//没有图片 返回正文高度 头像高度 之和
return size.height + 30 +35;
}
}

2. 汽车列表、LOL英雄列表 : 练习单元格的使用,以及重用问题
汽车展示:分组展示每个系列的车 右侧通过方法添加索引
英雄展示:可以点击单元格,修改英雄的名字
效果图:





