我正在尝试实现一个静态表格 View ,根据用户选择的选项,隐藏或显示不同的表格 View 单元格。
当“Repeat”为“Never”时,不应显示单元格,但当“Repeat”为其他任何内容时,应显示“Repeat End”单元格。
现在的问题是,当我选择从“从不”到任何其他值的重复值时{意味着我已将单元格的状态从隐藏更改为未隐藏} 单元格显示为空白。
我已经使用 heightForRowAt 实现了显示和隐藏
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let defaultHeight = super.tableView(tableView, heightForRowAt: indexPath)
let heightIndexPath: [Int: [Int: CGFloat]] = [1:
[sectionRow.startDateTimePicker.rawValue : startDateEditing ? startDateTimePickerCell.contentView.subviews.first?.frame.height ?? 0 : 0,
sectionRow.startDateTimeZone.rawValue : startDateEditing ? defaultHeight : 0,
sectionRow.endDateTimePicker.rawValue : endDateEditing ? endDateTimePickerCell.contentView.subviews.first?.frame.height ?? 0 : 0,
sectionRow.endDateTimeZone.rawValue : endDateEditing ? defaultHeight : 0,
sectionRow.repeatEnd.rawValue : repeatValue == Event.Repeat.Never ? 0 : defaultHeight]]
let cellHeight = heightIndexPath[indexPath.section]?[indexPath.row] ?? defaultHeight
let cell = self.tableView(tableView, cellForRowAt: indexPath)
cell.isHidden = cellHeight == 0 ? true : false
cell.alpha = cellHeight == 0 ? 0 : 1
return cellHeight
}
我什至尝试更改单元格的 isHidden 和 alpha 值,但它仍然没有显示。但奇怪的是,当我更改值(并非永远不会)时,单元格再次出现。
我是否错过了显示此隐藏单元格的内容?
顺便说一句,当重复值如下更改时,我只刷新隐藏的单元格。我什至尝试重新加载整个表格,但它没有用。
internal var repeatValue = Event.Repeat.Never {
didSet {
repeatLabel.text = repeatValue.rawValue
let indexPaths = [IndexPath(row: sectionRow.repeatEnd.rawValue, section: 1)]
tableView.reloadRows(at: indexPaths, with: .none)
// tableView.reloadData() // Tried this also.
}
}
最佳答案
您应该改写 tableView(tableView:numberOfRowsInSection:)
当你想隐藏行时,调用 super 实现然后减去 1。现在您将能够通过调用为行显示/隐藏设置动画
deleteRows(at:with:)
或 insertRows(at:with:)
视情况而定。
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
var numberOfRows = super.tableView(tableView, numberOfRowsInSection: section)
if (section == RepeatSection && shouldHideRepeatEnd()) {
numberOfRows = numberOfRows - 1
}
return numberOfRows
}
这样会好看很多
关于ios - 高度从 0 变为默认大小后单元格不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45890438/