美文网首页
UITableViewcell自适应高度解决方案

UITableViewcell自适应高度解决方案

作者: 曲年 | 来源:发表于2015-09-24 14:59 被阅读295次

方案1

1.首先在主方法里面注册Cell.

UINib *cellNib = [UINib nibWithNibName:@"C1" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"C1"];
self.prototypeCell= [self.tableView dequeueReusableCellWithIdentifier:@"C1"];

2.返回正常的cell,记住此时不需要创建cell.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
C1 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C1"];
cell.t.text = [self.tableData objectAtIndex:indexPath.row];
return cell;}

3.在返回高度方法返回高度.

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
C1 *cell = (C1 *)self.prototypeCell;
cell.translatesAutoresizingMaskIntoConstraints = NO;
cell.t.translatesAutoresizingMaskIntoConstraints = NO;
cell.i.translatesAutoresizingMaskIntoConstraints = NO;
cell.t.text = [self.tableData objectAtIndex:indexPath.row];
CGSize size=[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
NSLog(@"h=%f", size.height + 1);
return 1 + size.height;}

iOS7中系统先调用:
-(CGFloat)tableView:(UITableView)tableView heightForRowAtIndexPath:(NSIndexPath )indexPath这个方法,
再调用**
-(UITableViewCell *)tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath 。
** 这个方法。iOS8恰好相反。

方案2

相关文章

网友评论

      本文标题:UITableViewcell自适应高度解决方案

      本文链接:https://www.haomeiwen.com/subject/znoicttx.html