2012-04-25 83 views
9

这应该很简单,但我遇到了麻烦。如何从故事板中创建的静态UITableView中删除单元格

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

我有一个IBOutlet中它

IBOutlet UITableViewCell * cell15; 

而且我可以通过调用

cell15.hidden = true; 

这个隐藏它删除,但保留其中的细胞曾经是一片空白,我可以”摆脱它。

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

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

非常感谢!

+1

'tableView:deleteRowAtIndexPath:'? 没有自己尝试,只是一个快速的方法来试试 – anticyclope 2012-04-25 06:13:59

+0

谢谢!我将如何去选择我想要删除的行? – dot 2012-04-25 06:23:15

+0

[如何从StoryBoard中设计的UITableView中移除静态单元格]的可能重复(https://stackoverflow.com/questions/8262270/how-to-remove-a-static-cell-from-a-uitableview-designed故事板) – 2017-06-18 23:54:48

回答

12

在数据源中不能真正处理这个问题,因为使用静态表您甚至不实现数据源方法。高度是要走的路。

试试这个:

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

+0

在这种情况下,我得到了一个'BAD_ACCESS'。 TableView不会询问单元格的高度_before_实例化吗? – Besi 2012-07-23 09:33:27

+0

它以前会询问(在这种情况下,单元格将为零,并且会落入超级单元格中),而且在滚动期间,我认为,我不知道如何通过此代码获得不良访问权限。你应该发布一个新的问题,并附上这个答案的链接。 – jrturton 2012-07-23 09:40:49

+5

我也遇到了由某种无限循环引起的'BAD_ACCESS'。我通过不比较单元格来修正它,但索引路径是这样的:'if(indexPath.row == 3 && cellShouldBeHidden)' – codingFriend1 2013-07-17 10:00:52

3

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

更新的评论:

的部分参数将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; 
    } 
} 
+0

如何选择代码中的部分?这是我真正遇到的问题... – dot 2012-04-25 06:23:24

+0

这会在两个相邻(非隐藏)部分之间留下太大的间隙,尽管... – Drux 2013-08-24 19:26:21

6

您可以使用tableView:willDisplayCelltableView:heightForRowAtIndexPath,其小区标识显示/隐藏静态tableview细胞,但溜溜必须实现heightForRowAtIndexPathsuper,不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; 
} 
+0

作品完美! – mikemike396 2013-06-11 19:39:33

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]; 
} 
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]; 
} 
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; 
} 
相关问题