为了演示简单,cell中我们只包含了一个view和一个label,针对iOS8之前的iOS系统,我们需要进行如下设置:
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.estimatedRowHeight = 80;
[self.tableView registerClass:[HomePageCell class] forCellReuseIdentifier:NSStringFromClass([HomePageCell class])];
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomePageCell class])
forIndexPath:indexPath];
[self cell:cell setUpWithIndexPath:indexPath];
return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static HomePageCell *cell;
if (cell == nil) {
cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomePageCell class])];
};
[self cell:cell setUpWithIndexPath:indexPath];
return [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;
}
- (void)cell:(HomePageCell *)cell setUpWithIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = indexPath.row;
if (row % 2 == 0) {
cell.topView.backgroundColor = [UIColor redColor];
cell.titleLbl.text = @"孙杨世锦赛400米自由泳“三连冠” 叶诗文无缘200米混合泳决赛";
} else {
cell.topView.backgroundColor = [UIColor greenColor];
cell.titleLbl.text = @"中新社布达佩斯7月23日电 (记者 王婧)2017年国际泳联世锦赛游泳项目23日开赛。奥运冠军孙杨在男 子400米自由泳项目中夺得本届世锦赛游泳项目首金,中国“00后”小丫李冰洁在女子400米自由泳决赛摘铜,伦敦奥运冠军叶诗文无缘女子200米个人混合泳决赛。";
}
}
下面是HomePageCell中的关键代码:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_topView = [UIView new];
_topView.backgroundColor = [UIColor redColor];
[self addSubview:_topView];
[_topView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(self);
make.top.equalTo(self).with.offset(10);
make.height.equalTo(@(30));
}];
_titleLbl = [UILabel new];
_titleLbl.preferredMaxLayoutWidth = 100;
_titleLbl.numberOfLines = 0;
_titleLbl.lineBreakMode = NSLineBreakByCharWrapping;
[self addSubview:_titleLbl];
[_titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(_topView.mas_bottom);
make.left.equalTo(self);
make.width.equalTo(@(100));
make.bottom.equalTo(self);
}];
[_titleLbl setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
}
return self;
}
对于iOS8之后的版本,我们在tableView定义的时候要多加一句:
self.tableView.rowHeight = UITableViewAutomaticDimension;
在UITableViewDelegate代理方法,heightForRowAtIndexPath中直接返回UITableViewAutomaticDimension,代码如下:
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}