UITableViewcell自适应高度解决方案

方案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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容