use*_*700 10 objective-c uitableview ios
我正试图从UITableView隐藏一个单元格.就像删除动作一样,但我只想隐藏它以便稍后在同一位置显示它.
我知道UITableViewCell有一个名为"隐藏"的属性,但是当我使用这个属性隐藏Cell时,它隐藏但没有动画,它们留下一个空格
例:
有可能当我隐藏第二个细胞时,第三个细胞的位置变为2?
谢谢
Tim*_*sen 23
一种有效地"隐藏"动画行但没有实际移除它的方法是将其高度设置为零.你可以通过覆盖来实现-tableView:heightForRowAtIndexPath:.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 0.0;
if (isRowHidden) {
height = 0.0;
} else {
height = 44.0;
}
return height;
}
Run Code Online (Sandbox Code Playgroud)
(当然,您只想为要隐藏的特定行返回0.0,而不是无条件地返回).
简单地更改此方法的返回值不会使表视图自动调整行的高度,这样就可以进行以下调用.
isRowHidden = YES;
[tableView beginUpdates];
[tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
如果你这样做,你会看到两者之间的动画出现/消失过渡.
Zai*_*han 19
在SWIFT中,你需要做两件事,
隐藏你的牢房.(因为可重用的单元格可能会发生冲突)
将单元格的高度设置为ZERO.
看这里,
隐藏你的细胞.
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
}
Run Code Online (Sandbox Code Playgroud)将单元格的高度设置为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
}
Run Code Online (Sandbox Code Playgroud)使用此功能可以消除可重用的单元冲突问题.
您可以对单元格执行相同的操作吗?.tag也可以按标记隐藏特定的单元格.
参考:https://stackoverflow.com/a/28020367/3411787
如果您希望其他单元格具有动态高度:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 1 { // Or any other row
return 0
}
return -1.0
}
Run Code Online (Sandbox Code Playgroud)
与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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32273 次 |
| 最近记录: |