如何从Storyboard中创建的静态UITableView中删除单元格

dot*_*dot 10 storyboard uitableview ios

这应该很容易,但我遇到了麻烦.

我有一个带有单元格的静态UITableView,如果不需要,我想以编程方式删除它.

我有一个IBOutlet

IBOutlet UITableViewCell * cell15;
Run Code Online (Sandbox Code Playgroud)

我可以通过电话将其删除

cell15.hidden = true;
Run Code Online (Sandbox Code Playgroud)

这隐藏了它,但留下了一个空白的空间,细胞曾经是,我无法摆脱它.

也许黑客会将它的高度改为0?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:indexPath
{
//what would I put here?
}
Run Code Online (Sandbox Code Playgroud)

非常感谢!

jrt*_*ton 13

你无法在数据源中真正处理这个问题,因为对于静态表,你甚至都没有实现数据源方法.高度是要走的路.

试试这个:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == cell15 && cell15ShouldBeHidden) //BOOL saying cell should be hidden
        return 0.0;
    else
        return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
} 
Run Code Online (Sandbox Code Playgroud)

更新

看来,在自动布局下,这可能不是最好的解决方案.还有另外一种答案这里这可能会有帮助.

  • 我也有一个由某种无限循环引起的'BAD_ACCESS`.我通过不比较单元格而不是比较索引路径来修复它:`if(indexPath.row == 3 && cellShouldBeHidden)` (5认同)

小智 6

您可以使用tableView:willDisplayCelltableView:heightForRowAtIndexPath使用单元格标识符来显示/隐藏静态tableview单元格,但是您必须实现heightForRowAtIndexPath引用super,而不是self.这两种方法对我来说很好:

(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell.reuseIdentifier.description isEqualToString:@"cellCelda1"]) {
    [cell setHidden:YES];
    }
}
Run Code Online (Sandbox Code Playgroud)

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if ([cell.reuseIdentifier.description isEqualToString:@"cellCelda1"]) {
        return 0;
}
    return cell.frame.size.height;
}
Run Code Online (Sandbox Code Playgroud)