【问题标题】:How to remove cell from static UITableView created in Storyboard如何从 Storyboard 中创建的静态 UITableView 中删除单元格
【发布时间】:2012-05-05 19:32:06
【问题描述】:

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

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

我有一个 IBOutlet

IBOutlet UITableViewCell * cell15;

我可以通过调用删除它

cell15.hidden = true;

这隐藏了它,但在单元格曾经所在的位置留下了一个空白区域,我无法摆脱它。

也许黑客会将其高度更改为 0?

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

非常感谢!

【问题讨论】:

标签: ios uitableview storyboard


【解决方案1】:

根据您的表应该如何工作,您可以在数据源中实现tableView:numberOfRowsInSection:,以根据您的必要逻辑为该部分返回 0 行。

更新您的评论:

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

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

【讨论】:

  • 如何选择代码中的部分?这是我真正遇到的麻烦......
  • 这在两个相邻(非隐藏)部分之间留下了太大的间隙......
【解决方案2】:

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

试试这个:

- (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 可能会有所帮助。

【讨论】:

  • 在这种情况下,我收到了BAD_ACCESS。 TableView 不是在实例化单元格之前询问高度吗?
  • 它会在之前询问(在这种情况下,单元格将为 nil,并进入超级单元)以及在滚动期间,我想,我不明白你会如何获得错误的访问权限使用此代码。您应该发布一个新问题,并附上此答案的链接。
  • 我还收到了由某种无限循环引起的BAD_ACCESS。我通过不比较单元格而是索引路径来修复它:if (indexPath.row == 3 && cellShouldBeHidden)
  • 同意,这将循环并给你 BAD_ACCESS,这不应该是这个问题的公认答案。
【解决方案3】:

您可以使用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;
}

【讨论】:

    【解决方案4】:

    只适用于恒定电池

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

    【讨论】:

      【解决方案5】:

      您可以做的第一件事是从情节提要中标记要隐藏的单元格。 输入一些你可以识别的标准数字。

      添加此代码。

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

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

          【解决方案7】:

          使用索引路径识别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)
          }
          

          【讨论】:

            猜你喜欢
            • 2012-01-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-05-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多