在静态表格视图中隐藏单元格

时间:2021-03-25 10:45:28

标签: objective-c uitableview

我在 IB 中有一个静态表格视图,每个单元格的插座类型为 UITableViewCell。我试图通过设置 0 高度来隐藏特定的单元格。我正在使用给定插座的 indexPathForCell。问题是 indexPathForCell 返回 nil。代码如下:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath compare:[tableView indexPathForCell:self.cellSelfView]] == NSOrderedSame) {
        return 0.0;
    }
    return UITableViewAutomaticDimension;
}

我知道的唯一解决方案是将 indexPath.row 与特定索引进行比较,但该解决方案并不可靠。任何隐藏单元格的想法都值得赞赏。

1 个答案:

答案 0 :(得分:0)

我发现委托方法 willDisplayCell 是在显示单元格之前进行最后一分钟视觉更改的最合适的地方。

在你的情况下,它会是这样的:

- (void) tableView: (UITableView *) tableView willDisplayCell: (UITableViewCell *) cell forRowAtIndexPath: (NSIndexPath *) indexPath {
    if indexPath.row == // do your check here to see if the cell should be hidden {
       cell.frame = CGRectMake(0.0, 0.0, cell.size.width, 0.0); // set a zero height to hide the cell
    }
}