iOS Cell动态行高

在iOS开发中,cell动态行高的设置无疑很让人抓狂,本文在此分享一个cell行高的动态获取方法;
首先,本文介绍的方法适用于直接new创建cell的情况,代码如下:
cell样式代理方法中添加:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      //关键部分
      CGRect rect = cell.frame;
      //getCellHeight方法为自定义Cell中获取布局高度的方法
      rect.size.height = [cell getCellHeight];
      cell.frame = rect;
}

注意,Label的高度自适应设置,本文略过,如要参考请移步链接:http://www.jianshu.com/p/37a8414e015a
设置Cell的height,实现table的cell高度代理:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 280;
    }else if (indexPath.row == 1){
        return 44;
    }else{
        //动态计算行高 - wsx注释
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];//获取对象cell
        return cell.frame.size.height;
    }
}

这样就能实现cell的动态行高了;
荆轲刺秦王!

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

推荐阅读更多精彩内容