29

我在 grouptableview 中有一个自定义的 tableview 单元格。我有一个隐藏的。然后我必须让它可见。单元格标记为 3。

这不适用于我的代码:

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

只是我需要让一行可见。我希望你明白。

4

4 回答 4

53

SWIFT中,您需要做两件事,

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

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

看这里,

  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. 将单元格的高度设置为零

    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执行相同的操作,也可以按标签隐藏特定的单元格。

于 2015-01-19T08:17:53.310 回答
50

在 中传递该zero特定单元格的单元格高度heightForRowAtIndexPath:,它将自动隐藏:-

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

将以下方法添加到您的代码中,它就可以解决问题。希望它会帮助你。

于 2013-05-05T17:25:14.753 回答
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;
  }

希望这对你有帮助

于 2013-05-05T17:25:28.067 回答
1

图片[![][1] 在此处输入图像描述

这是我的场景。首先,我的表格视图是静态的。在“帐户”部分中,应该始终只显示一个单元格。登录时显示 LoggedInCell,未登录时显示 unLoggedInCell。一种解决方案是将它们的高度设置为零,但您可能会遇到 NSContraints 错误,该错误很难修复。我的解决方案如下:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let count = super.tableView(tableView, numberOfRowsInSection: section)
    if section == 0 {
        return count - 1
    }
    return count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.section == 0 {
        if userForApp == nil {
            return super.tableView(tableView, cellForRowAt: IndexPath(row: 0, section: 0))
        } else {
            return super.tableView(tableView, cellForRowAt: IndexPath(row: 1, section: 0))
        }
    } else {
        return super.tableView(tableView, cellForRowAt: indexPath)
    }

}

非常简单!是的?顺便说一句,您可能像我一样有单元格高度问题,我的意思是两个单元格(UnLoggedInCell 和 LoggedInCell)具有不同的高度,我们应该通过这样做告诉表格视图对象单元格高度的值:

    var originHeightOfUnLoggedInCell: CGFloat = 0.0
    var originHeightOfLoggedInCell: CGFloat = 0.0

    func recordInitialHeightOfCells() { // called in viewDidLoad()
        self.originHeightOfUnLoggedInCell = self.unLoggedInCell.frame.height
        self.originHeightOfLoggedInCell = self.loggedInCell.frame.height
  }

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            if userForApp == nil {
                return originHeightOfUnLoggedInCell
            } else {
                return originHeightOfLoggedInCell
            }
        } else {
            return super.tableView(tableView, heightForRowAt: indexPath)
        }
    }
于 2019-05-04T08:44:19.033 回答