Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.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 复杂单元的动态UITableViewCell高度_Ios_Dynamic_Uitableview - Fatal编程技术网

Ios 复杂单元的动态UITableViewCell高度

Ios 复杂单元的动态UITableViewCell高度,ios,dynamic,uitableview,Ios,Dynamic,Uitableview,我试图在UITableView中计算单元格的动态高度 我的单元格非常复杂:可能有十几个标签、uitextfield、按钮等。事实上,它就像单元格中的一个表单(使用de-UITableViewGroupedStyle,它看起来真的像一个表单!)。所以我不在乎“sizeWithFont:withConstrainedSize:etc…” 我的单元格中有一个自定义类方法,用于计算单元格的高度(从内部组件的高度) 现在,在我的UITableViewController中,我想为我的单元格设置正确的高度。

我试图在UITableView中计算单元格的动态高度

我的单元格非常复杂:可能有十几个标签、uitextfield、按钮等。事实上,它就像单元格中的一个表单(使用de-UITableViewGroupedStyle,它看起来真的像一个表单!)。所以我不在乎“sizeWithFont:withConstrainedSize:etc…”

我的单元格中有一个自定义类方法,用于计算单元格的高度(从内部组件的高度)

现在,在我的UITableViewController中,我想为我的单元格设置正确的高度。为此,我需要在手机上拨打getHeight。但是如果我在“heightForRowAtIndexPath”中调用“cellForRowAtIndexPath”,它会循环(因为cellForRow…需要heightForRow…来构建单元格)

我还尝试在cellForRowAtIndexPath中构建该单元格,获取其高度并将其存储在数组中,以在heigtForRowAtIndexPath中检索该单元格。因为在“cellForRow…”之前调用了“heightForRowAtIndexPath”,所以我第一次返回默认值(CGFLOAT_MAX)。构建单元时,我返回从阵列检索到的单元高度。但是要第二次调用它,我需要手动调用它,所以当我的单元格内置在“CellForRow…”中时,我调用reloadData。但是(一次又一次)反正也不行。。。事实上,有时它有效(使用UITableViewGroupedStyle),有时则无效(使用UITableViewPlainStyle,因为它与CGFLOAT\U最大默认值类似…)

这让我发疯

此版本适用于UITableViewGroupedStyle:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MFFormListCell *cell = [self.cellSizeMap objectForKey:indexPath];
    return cell;

}

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

    MFFormListCell *cell = [self.cellSizeMap objectForKey:indexPath];
    if(!cell || ![cell isEqual:[NSNull null]]) {
        [self.cellSizeMap setObject:[NSNull null] forKey:indexPath];
        cell = [tableView dequeueReusableCellWithIdentifier:@"FormListCell" forIndexPath:indexPath];
        [self.cellSizeMap setObject:cell forKey:indexPath];
    }


    if([cell isEqual:[NSNull null]])
        return CGFLOAT_MAX;     //Valeur max pour que le système prennent en compte toutes les cellules à afficher
    else
        return [[cell getHeight] floatValue];


}

不要在heightForRowAtIndexPath:中询问第一个“if”,这是避免循环的一个坏技巧。

您的表数据源应该知道单元格的高度,而不是单元格的高度。根据将显示的内容计算视图控制器中每个单元格的高度,并将其存储在数组中。重新加载表时,只需在相应索引处返回数组中先前计算的高度的索引

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
     return [_cellHeights objectAtIndex:indexPath.row];
}

显示您的cellforrowatindexpath方法。我想看看你的代码。我经常修改它,所以现在没有稳定的版本。