Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在CellForRowatineXpath之前调用HeightForRowatineXpath_Ios_Objective C_Uitableview_Tableviewcell - Fatal编程技术网

Ios 在CellForRowatineXpath之前调用HeightForRowatineXpath

Ios 在CellForRowatineXpath之前调用HeightForRowatineXpath,ios,objective-c,uitableview,tableviewcell,Ios,Objective C,Uitableview,Tableviewcell,我试图在布局tableview时减少重复代码,但遇到了生命周期问题。基本上,heightFroRowAtIndexPath是在cellForRowAtIndexPath之前调用的。这就是应该发生的事情,我明白为什么 但是 我有一个故事板上的单元。它有一个可选字段。如果可选字段不在数据中,则删除该字段的标签。但是,我正在自定义单元格实现中删除该标签: CustomCell(扩展UITableViewCell) 然后在cellForRowAtIndexPath中: - (UITableViewCel

我试图在布局tableview时减少重复代码,但遇到了生命周期问题。基本上,heightFroRowAtIndexPath是在cellForRowAtIndexPath之前调用的。这就是应该发生的事情,我明白为什么

但是

我有一个故事板上的单元。它有一个可选字段。如果可选字段不在数据中,则删除该字段的标签。但是,我正在自定义单元格实现中删除该标签:

CustomCell(扩展UITableViewCell)

然后在cellForRowAtIndexPath中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:self.tableLayout[indexPath.section][indexPath.row]];
        [cell configureCellForData:self.data];
        return cell;
}
这对设置电池非常有效。但是,如果删除了可选字段,则高度是错误的,即如果删除了可选字段,则我需要进行调整

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:self.tableLayout[indexPath.section][indexPath.row]];
        CustomCell *headerCell = (CustomCell *) cell;
        if (self.data.optional == nil) {
            return cell.bounds.size.height - headerCell.optionalLabel.bounds.size.height;
        }

        return cell.bounds.size.height;
    } 
}
看起来不太像,但我将检查简化为“data.optional==nil”,它比这更复杂,并且涉及一个DB调用


有没有更好的方法来设置它,这样我就不必在计算单元格高度和初始化单元格时进行两次检查

如果只想检查一次,则可以存储一个布尔数组,用于存储数据是否存在。因此,对每一行进行检查,将结果存储到数组中,在下次进行检查之前,检查数组是否有该单元格的值,如果有,则使用该值,如果没有,则进行数据库调用

确保只在与indexPath关联的数组索引中存储值,如果数组短于当前的indexPath,则需要进行调用并将值添加到数组中


编辑:当我想得更多时,我会将bool值放在单元格本身上,然后只调用
cell.isDataAvailable
(或任何您想要的值),以避免在设置单元格时进行第二次调用,由于您已经在heightForRowAtIndexPath中选中了此选项。

是否可以使用自动布局并让单元格内容确定高度,而不是手动计算行高度?这看起来很有希望,但实施起来却有困难。在不希望单元格显示的某些情况下,我仍然需要覆盖heightForRowAtIndexPath以返回0。似乎当我重写heightForRowAtIndexPath时,我需要返回一些内容,因为“自动高度”不起作用。此外,在阅读本文后,这似乎对定义良好的标签内的动态内容非常有效。不确定如何在我从视图中删除标签时使其大小正确。是-如果使用“自动高度”,您将不再需要
heightforrowatinexpath
。如果不希望单元格显示,可以将其从数据源中删除。当涉及到隐藏标签时,您可以将标签的各个高度更新为0,而不是完全从视图中删除标签,这样可以有效地隐藏标签,但也可以使用自动布局。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:self.tableLayout[indexPath.section][indexPath.row]];
        CustomCell *headerCell = (CustomCell *) cell;
        if (self.data.optional == nil) {
            return cell.bounds.size.height - headerCell.optionalLabel.bounds.size.height;
        }

        return cell.bounds.size.height;
    } 
}