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

时间:2012-04-25 06:04:35

标签: ios uitableview storyboard

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

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

我有一个IBOutlet

IBOutlet UITableViewCell * cell15;

我可以通过调用

删除它
cell15.hidden = true;

这隐藏了它,但留下了一个空白区域,细胞曾经存在,我无法摆脱它。

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:indexPath
{
//what would I put here?
}

非常感谢!

7 个答案:

答案 0 :(得分: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]; 
} 

<强>更新

看来,在自动布局下,这可能不是最佳解决方案。有一个替代答案here可能有所帮助。

答案 1 :(得分: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];
    }
}

(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;
}

答案 2 :(得分:3)

根据表的工作方式,在数据源中,您可以根据必要的逻辑实现tableView:numberOfRowsInSection:以返回0行。

更新了您的评论:

在调用您的实现时,iOS将填充section参数,因此您只需要一个开关来处理包含您删除/隐藏行的部分。示例如下:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch(section) {
        case 0:  // first section of your table, change for your situation
             return 0;
        default:
             return 0;
    }
}

答案 3 :(得分:0)

仅适用于恒定单元格

-(void)tableViewSearchPeopleCellHide:(BOOL)hide{

    searchCellShouldBeHidden=hide;
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    cell.hidden=hide;
    self.searchPeople.hidden=hide;//UILabel
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (searchCellShouldBeHidden) //BOOL saying cell should be hidden
        return 0.0;
    else
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

答案 4 :(得分:0)

您可以做的第一件事就是从要隐藏的故事板中标记单元格。 放一些你可以识别的标准号码。

添加此代码。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (cell.tag==10) { //I have put 10 for some static cell.       
                cell.hidden=YES;
                return 0;         

    }
     cell.hidden = NO;
    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

答案 5 :(得分:0)

将要隐藏的单元格设置为隐藏在代码中的某个位置。添加此代码:(如果您的单元格具有不同的行高,则需要覆盖更多函数)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int rowCount=0;
    for ( int row=0; row<[super tableView:tableView numberOfRowsInSection:section]; ++row){
        NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:section];
        UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path];
        if (!cell.hidden){
            ++rowCount;
        }
    }
    return rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    int realRow=-1;
    for ( int row=0; row<[super tableView:tableView numberOfRowsInSection:indexPath.section]; ++row){
        NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:indexPath.section];
        UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path];
        if (!cell.hidden){
            ++realRow;
        }
        if (realRow==indexPath.row)
            return cell;
    }
    return nil;
}

答案 6 :(得分:0)

使用索引路径标识tableview高度委托中的单元格并返回0

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if someCondition {

            if indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 {
                return 0 
            }

        }else{

            if indexPath.row == 4 {
                return 0 
            }

        }


    return super.tableView(tableView, heightForRowAt: indexPath)
}
相关问题