iOS实现Cell自适应高度

我是研究僧i 提交于 2019-11-29 08:36:31

1.实现UITableViewDelegate中的方法

先设置cell的contentview中label根据内容自动换行
numberOfLines=0
实现UITableViewDelegate中的方法
- (CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath{
return UITableViewAutomaticDimension;
}
2.根据content view设置cell的frame
先设置cell的contentview中label根据内容自动换行
numberOfLines=0
cell根据Label大小 修改cell此时的尺寸
CGRect bounds = cell.contentLb.bounds;
bounds.size.height = bounds.size.height + 10;
cell.bounds = bounds;
或者在viewDidLoad里写下这两句

self.chatTableView.rowHeight = UITableViewAutomaticDimension;
self.chatTableView.estimatedRowHeight = 70;//这句必须写上值可以为自己估算的cell的高

转载于:https://my.oschina.net/zyboy/blog/617428

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!