0

我需要以编程方式仅设置一行的高度,所有其他行都是在情节提要中创建的具有不同高度的静态单元格。

在我的代码中,第 5 个单元格应默认为“隐藏”,因此我想将此行的高度设置为 0.0。所有其他单元格都应该使用它们在情节提要中的高度,我正在使用该方法tableView:heightForRowAtIndexPath来设置单元格的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = self.tableView.rowHeight;

    if (indexPath.row == 4)
    {
        height = 0.0f;
    }

    return height;
}

问题是self.tableView.rowHeight总是返回658高度,这是一个完全错误的值。

4

1 回答 1

0

如果你实现heightForRowAtIndexPath你需要为所有单元格设置一个值,否则你会遇到这样的事情。您可以执行以下操作:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     switch (indexPath.row){
           case 4:
                return 0;

           default:

               static NSNumber *height;
               UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"];
               height = @(cell.bounds.size.height);
               return [height floatValue];
      }
 }
于 2014-03-17T15:42:48.500 回答