You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何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 SE模拟器运行效果

iPhone 13 模拟器

表格行一旦被隐藏后,就无法再次显示:
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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 04:45:36