0%

iOS开发:解决UITableView的tableHeaderView与cell内容重叠以及cell只显示部分内容

cell有多行但只显示一行,报错 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.显然是iOS认为我的cell没有高度,搜索了好多文章,基本上都是用

- (CGFloat)tableView:(UITableView *)tableView
estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}

给cell设置高度,但我每个cell的高度都不是固定的,需要动态计算,既然已经用了AutoLayout,感觉实现起来很渣。 偶然发现原因竟然是没有给最下面的view设置与superview的margin,iOS这个设计就有点傻,superview的高度在没有约束的时候,就不会自动根据content算。加上以后,两个问题解决。动态cell高度,需要加上

    tableView.estimatedRowHeight = 110
    tableView.rowHeight = UITableViewAutomaticDimension

预估的高度与实际接近就行