cell自适应高度,在使用static cells时的一些用法

1.一般我们在使用动态cell的时候在viewdidLoad里加上这两句代码就可以了

self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 80;

2.但是当使用静态cell的时候加上这两句代码好像并不起作用这时就要用下面的代码

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if (indexPath.row == 0) {
    return UITableViewAutomaticDimension;

    }else{
    return 44;
    }

    }

  • (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return 200;
    }

3. 有时会出现这样的警告

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.

这是因为我只写了下面这行代码

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {

    return UITableViewAutomaticDimension;
    }

但是我总共有两个静态cell,这样写就可以了

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if (indexPath.row == 0) {
    return UITableViewAutomaticDimension;

    }else{
    return 44;
    }

    }
    主要是因为我第一个cell从上到下加好约束了,即top,bottom,而第二个cell因为不需要自适应高度,就没有保证从顶部到底部的约束,这样就会出现警告。
    详细的请看

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容