我一直在设置我的表格 View 的单元格高度
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.section {
case 0:
return 100
case 1:
tableView.estimatedRowHeight = 100
return UITableViewAutomaticDimension
default:
return 0
}
}
这样,对于第 1 部分中的所有行,行高设置为 100。现在,我想在单击按钮时将特定行的行高更改为 0(即使其隐藏)。
我已经尝试过:
tableView.cellForRow(at: IndexPath(item: selectedIndex, section: 1))?.isHidden
虽然单元格被隐藏,但我留下了一个占据行高的空白单元格。
所以我认为我应该将行高更改为 0。
附注我知道这可以通过从模型中删除特定行并使用deleteRows(at indexPaths: [IndexPath], 带动画:UITableView.RowAnimation)但我不想更改模型。如何做到这一点?
最佳答案
使用标志来检查您是否单击了该按钮。然后单击按钮后重新加载表格。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return indexPath.section == 0 ? buttonClicked && indexPath.row == 0 ? CGFloat.leastNormalMagnitude : 100 : UITableViewAutomaticDimension
}
更长、更易读的版本:-
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.section {
case 0:
if buttonClicked && indexPath.row = rowIndex //index of required row which you can find out and store in a variable wherever necessary {
return CGFloat.leastNormalMagnitude
}
return 100
case 1:
tableView.estimatedRowHeight = 100
return UITableViewAutomaticDimension
default:
return 0
}
}
关于ios - 更改节中特定索引的行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51298614/