2013-05-05 136 views
24

我在grouptableview中有一个自定义tableview单元格。我有一个隐藏的。然后我必须使其可见。电池标签为3UITableView设置tableview行隐藏

这不是我的工作代码:

if (self.tableView.tag == 3) { 
       self.tableView.hidden = NO; //Not working. 
      } 

正是我需要做一个行可见。我希望你明白。

+0

参考这篇文章 http://stackoverflow.com/questions/2670635/hiding-uitableviewcell – icodebuster 2013-05-05 17:10:38

+1

你在那里隐藏你的整个的tableView,而不是一行。 – memmons 2013-05-05 17:24:33

回答

39

传递单元格高度zeroheightForRowAtIndexPath:特定的细胞,它会自动获得隐藏: -

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     float heightForRow = 40; 

     YourCustomCell *cell =(YourCustomCell *)[tableView cellForRowAtIndexPath:indexPath]; 

     if(cell.tag==3) 
      return 0; 
     else 
      return heightForRow; 
} 

添加下面的方法到您的代码,它会做的伎俩。 希望它能帮助你。

+1

这对我有用。我确实将我的代码更改为float floatForRow = tableView.rowHeight;如果该属性未设置,则该属性返回默认高度。 – 2014-03-10 18:58:49

+5

自cellForRowAtIndexPath以来,这不起作用:导致它崩溃,因为单元格不存在。在创建单元格之前设置高度。 – Joey 2014-05-20 18:21:58

+0

根据我的说法,这不是一个好的解决方案,因为当单元格被重用时可能会导致冲突。 – 2015-01-19 07:43:55

1

请参考下面的代码: -

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 
{ 
    if(section == theSectionWithoutARow) 
{ 
    if(shouldRemoveTheRow) 
     return [theArrayWithTheSectionContents count] - 1; 
    else 
     return [theArrayWithTheSectionContents count]; 
    } 
    // other sections, whatever 
    } 

    - (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:   (NSIndexPath *)indexPath 
    { 
     // blah blah standard table cell creation 

    id theCellObject; 

    if(indexPath.section == theSectionWithoutARow) 
    { 
    NSInteger theActualRowToDisplay = indexPath.row; 
    if(shouldRemoveTheRow && indexPath.row >= theRowIndexToRemove) 

    { 
     theActualRowToDisplay = indexPath.row + 1; 
    } 
    theCellObject = [theArrayWithTheSectionContents objectAtIndex:theActualRowToDisplay]; 
} 

// now set up the cell with theCellObject 

return cell; 
    } 

希望这有助于你

+13

可能是我读过的最杂乱的代码 – Osa 2015-11-13 18:42:23

41

SWIFT你需要做两件事情,

  1. 隐藏你的细胞。 (因为可重复使用的单元格可能冲突)

  2. 将单元格的高度设置为ZERO

在这里看看,

  1. 隐藏你的细胞。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
        let myCell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellID",for: indexPath) as! UITableViewCell 
    
        if(indexPath.row < 2){ 
         myCell.isHidden = true 
        }else{ 
         myCell.isHidden = false 
        } 
    
        return myCell 
    } 
    
  2. 细胞的设定高度,以ZERO

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
        var rowHeight:CGFloat = 0.0 
    
        if(indexPath.row < 2){ 
         rowHeight = 0.0 
        }else{ 
         rowHeight = 55.0 //or whatever you like 
        } 
    
        return rowHeight 
    } 
    

使用此您可以删除重复使用的细胞发生冲突的问题。

对于cell?.tag,您也可以通过标记隐藏特定的单元格。

+0

更好的答案 – 2015-03-09 09:02:30

+0

当我使用这个我得到一个警告:“可能至少有一个在下面的约束列表是你不想要的。试试这个:(1)看看每个约束,并试图找出你不期望的; (2)找到添加不需要的约束或约束的代码并修复它“ – DrPatience 2015-09-01 14:17:02

+2

我不明白为什么需要隐藏单元格?如何将高度设置为零会导致可重用单元格发生冲突? – 2015-09-15 15:03:22