ios UITableView 设置 tableview 行隐藏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16386939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:27:50  来源:igfitidea点击:

UITableView set tableview row hidden

iosobjective-cuitableview

提问by Salieh

I have a custom tableview cell in grouptableview. And I have one hidden. I then have to make it visible. Cell tag is 3.

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

This is not working my code:

这不起作用我的代码:

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

Just i need make a one row is visible. I hope you understand.

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

回答by Zaid Pathan

In SWIFTyou need to do two things,

SWIFT 中,您需要做两件事,

  1. HIDEyour cell. (because reusable cell may conflict)

  2. Set Height of cell to ZERO.

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

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

Look at here,

看这里,

  1. HIDEyou cell.

    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. Set Height of cell to 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
    } 
    
  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
    } 
    

Using this you can remove reusable cell conflict issues.

使用它,您可以消除可重用单元格冲突问题。

You can do the same for cell?.tagalso to hide specific cell by tag.

您可以对cell?.tag执行相同的操作,也可以按标签隐藏特定的单元格。

回答by Gaurav Rastogi

Pass the cell height zerofor that specific cell in the heightForRowAtIndexPath:, it will automatically get hidden:-

在 中传递该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;
 }

Add the following method to your code , it will do the trick . Hope it will help you .

将以下方法添加到您的代码中,它会起作用。希望它会帮助你。

回答by Arpit Kulshrestha

please refer this code :-

请参考此代码:-

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

Hope this help you

希望这对你有帮助

回答by De Zheng

Images[![][1]enter image description here

图片[![][1]在此处输入图片说明

Here is my scenario. First of all, my table view is static. And in section "Account", there should alway be only one cell displayed. LoggedInCell is displayed when user is logged in, and unLoggedInCell when user is not logged in. One solution is to set their height zero, but you may encounter NSContraints error which is complex to be fixed. My solution is below:

这是我的场景。首先,我的表视图是静态的。在“帐户”部分,应该始终只显示一个单元格。用户登录时显示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)
    }

}

quite simple! yes? Btw, You may have the cells height problem like me, I mean the two cells (UnLoggedInCell and LoggedInCell) have different height, we should tell the table view object that the value of cells height by doing this:

非常简单!是的?顺便说一句,您可能和我一样有单元格高度问题,我的意思是两个单元格(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)
        }
    }