为何UITableView行显示/隐藏机制在非iPhone SE模拟器中失效?
UITableView行显示/隐藏的跨机型异常问题
我通过以下代码实现UITableView行的显示与隐藏:
class TableViewController: UITableViewController { private var hiddenIndexPaths : Set<IndexPath> = [] override func viewDidLoad() { super.viewDidLoad() } @IBAction func toggle(_ sender: UISwitch) { if sender.isOn { show(1) show(2) } else { hide(1) hide(2) } tableView.beginUpdates() tableView.endUpdates() } private func isHidden(_ indexPath: IndexPath) -> Bool { hiddenIndexPaths.contains(indexPath) } private func hide(_ item: Int) { hiddenIndexPaths.insert(IndexPath(item: item, section: 0)) } private func show(_ item: Int) { hiddenIndexPaths.remove(IndexPath(item: item, section: 0)) } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if isHidden(indexPath) { return 0.0 } return super.tableView(tableView, heightForRowAt: indexPath) } }
运行表现
iPhone SE 模拟器(及真机)
该实现运行正常,行可正常切换显示/隐藏状态:
iPhone 13 模拟器
表格行一旦被隐藏后,就无法再次显示:
由于没有iPhone 13真机,无法确认真机上的表现,特此询问该问题产生的原因。完整可测试示例项目已上传至GitHub仓库。
测试环境
- XCode Version 14.0 (14A309)
- macOS Monterey Version 12.6 (M1)
- iPhone 13 模拟器,iOS 15
内容的提问来源于stack exchange,提问作者Cheok Yan Cheng
相关产品推荐
相关产品推荐

