iOS UITableView 解决估算行高和指定行高的矛盾

Posted 宁静暖风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS UITableView 解决估算行高和指定行高的矛盾相关的知识,希望对你有一定的参考价值。

1、一般来说 在ios 中若UITableViewCell 固定行高, 会通过

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

来设置;

2、如果需要由系统自动估算行高, 可以通过设置以下代码实现:

// 动态行高
        self.tableView.rowHeight = UITableViewAutomaticDimension;
        // 预估行高
        self.tableView.estimatedRowHeight = 150;

3、如果一个tableview即想用固定行高又想用预估行高(自动计算行高) 这两者的矛盾怎样解决呢?以下代码 关键在于 这句话:

return UITableViewAutomaticDimension;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 || indexPath.section == 1) {
            return 81;
    }
    // 注意:解决固定行高和系统自动计算行高  其他组走系统自动计算行高
    return UITableViewAutomaticDimension;
}

 

以上是关于iOS UITableView 解决估算行高和指定行高的矛盾的主要内容,如果未能解决你的问题,请参考以下文章