ios 隐藏 UITableview 单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29886642/
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
Hide UITableview cell
提问by user1628700
i'm trying to Hide a cell from a UITableView. just like the delete action do but i just want to hide it to later show it in the same position.
我正在尝试从 UITableView 隐藏单元格。就像删除操作一样,但我只想隐藏它以便稍后在同一位置显示它。
i know that UITableViewCell has a property called "Hidden" but when i hide the Cell using this property it hide but no animated and they leave a blank space
我知道 UITableViewCell 有一个名为“Hidden”的属性,但是当我使用此属性隐藏 Cell 时,它隐藏但没有动画,并且它们留下了空白
Example:
例子:
- first cell
- second cell
- third cell
- 第一个单元格
- 第二个单元格
- 第三个单元格
it's possible that when i hide the second cell, that third cell change position to 2 ?
有可能当我隐藏第二个单元格时,第三个单元格将位置更改为 2 吗?
thanks
谢谢
回答by Tim Johnsen
One way to effectively "hide" a row with animation and without actually removing it is to set it's height to zero. You'd do so by overriding -tableView:heightForRowAtIndexPath:
.
有效地“隐藏”带有动画的行而不实际删除它的一种方法是将其高度设置为零。您可以通过覆盖-tableView:heightForRowAtIndexPath:
.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 0.0;
if (isRowHidden) {
height = 0.0;
} else {
height = 44.0;
}
return height;
}
(You'd only want to return 0.0 for the specific row or rows you want to hide of course, not unconditionally).
(当然,您只想为要隐藏的特定行或多行返回 0.0,而不是无条件地)。
Simply changing the return value of this method doesn't make the table view automatically adjust the row's height, to make it do so you make the following calls.
简单地改变这个方法的返回值不会让 table view 自动调整行的高度,为了做到这一点,你可以进行以下调用。
isRowHidden = YES;
[tableView beginUpdates];
[tableView endUpdates];
If you do so you'll see an animated appearance/disappearance transition between the two.
如果这样做,您将看到两者之间的动画外观/消失过渡。
回答by Zaid Pathan
In SWIFTyou need to do two things,
在SWIFT 中,您需要做两件事,
HIDEyour cell. (because reusable cell may conflict)
Set Height of cell to ZERO.
隐藏你的手机。(因为可重复使用的单元格可能会发生冲突)
将单元格的高度设置为零。
Look at here,
看这里,
HIDEcell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) if indexPath.row == 1 { cell?.hidden = true } else { cell?.hidden = false } return cell }
Set Height of cell to ZERO.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { var rowHeight:CGFloat = 0.0 if(indexPath.row == 1){ rowHeight = 0.0 } else { rowHeight = 55.0 //or whatever you like } return rowHeight }
隐藏单元格
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) if indexPath.row == 1 { cell?.hidden = true } else { cell?.hidden = false } return cell }
将单元格的高度设置为零。
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { var rowHeight:CGFloat = 0.0 if(indexPath.row == 1){ 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 John Rogers
You can't simply hide a UITableViewCell. You have to remove that cell from the table view then insert it again when you would like it to reappear in the table.
你不能简单地隐藏一个 UITableViewCell。您必须从表格视图中删除该单元格,然后在您希望它重新出现在表格中时再次插入它。
The methods you're looking for are deleteRowsAtIndexPaths:withRowAnimation:
and insertRowsAtIndexPaths:withRowAnimation:
. These are well documented in the UITableView documentation here. You're going to have to remember where you removed the cell from then insert it at the same position later.
您正在寻找的方法是deleteRowsAtIndexPaths:withRowAnimation:
和insertRowsAtIndexPaths:withRowAnimation:
。这些在此处的 UITableView 文档中有详细记录。您将不得不记住从何处移除单元格,然后将其插入相同的位置。
Keep in mind that if you add cells to your table with a lesser row index than the row index of the deleted row, you will have to add on to the index to maintain it's relative position.
请记住,如果将行索引小于已删除行的行索引的单元格添加到表中,则必须添加到索引以保持其相对位置。
Hope this helps.
希望这可以帮助。
回答by Grzegorz R. Kulesza
Same as Zaid Pathanbut for Swift 4:
与Zaid Pathan相同,但适用于 Swift 4:
//HIDE you cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! UITableViewCell
//hide second cell
indexPath.row == 1 ? (cell.isHidden = true): (cell.isHidden = false)
return myCell
}
//Set Height of cell to ZERO.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var rowHeight:CGFloat = 0.0
indexPath.row == 1 ? (rowHeight = 0.0): (rowHeight = 49.0)
return rowHeight
}
回答by Micha? Ziobro
I am using Hidden in Attributes Inspector of TableViewCell -> ContentView and then implement method:
我在 TableViewCell -> ContentView 的属性检查器中使用 Hidden 然后实现方法:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if shouldHideSection(indexPath.section) {
return 0.0
} else if let isHidden = tableView.cellForRow(at: indexPath)?.contentView.isHidden, isHidden {
return 0.0
}
return super.tableView(tableView, heightForRowAt: indexPath)
}
回答by Binladenit Tran
You should delete row and reload table view [tbv reloadData];
您应该删除行并重新加载表视图 [tbv reloadData];
回答by taitanxiami
cell display, depending on the data source, so you need to deal with the data source.
单元格显示,取决于数据源,所以需要对数据源进行处理。
if dataArr is the table view datasource,first of all, you should delete the data source where the cell,then
如果dataArr是表视图数据源,首先你应该删除单元格所在的数据源,然后
[dataArr removeObjectAtIndex:indexpath.row];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
at last ,you can insert data source when you should add a cell
最后,您可以在添加单元格时插入数据源
[dataArr insertObjectAtIndex:indexpath.row];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
回答by lukaskasa
If you want the other cells to have a dynamic height:
如果您希望其他单元格具有动态高度:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 1 { // Or any other row
return 0
}
return -1.0
}
回答by Davender Verma
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let dicttemp : Dictionary = arrAllCoursesList[indexPath.row] as! Dictionary<String,Any>
var rowHeight:CGFloat = 0.0
if let getStatus = dicttemp["status"] as? String
{
if getStatus == "1"
{
rowHeight = 240.0
}
else
{
rowHeight = 0.0
}
}
return rowHeight
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CompetitionTableViewCell") as! CompetitionTableViewCell
let dicttemp : Dictionary = arrAllCoursesList[indexPath.row] as! Dictionary<String,Any>
if let getStatus = dicttemp["status"] as? String
{
if getStatus == "1"
{
cell.isHidden = false
}
else
{
cell.isHidden = true
}
}
cell.selectionStyle = UITableViewCellSelectionStyle.none
return cell
}