You need to enable JavaScript to run this app. 为什么SwiftUI列表滚动后选中行丢失且触发table view断言?
优惠活动
大模型
产品
解决方案
定价
更多

为什么SwiftUI列表滚动后选中行丢失且触发table view断言?

列表滚动选中状态丢失问题说明

运行环境

  • 开发工具版本:Xcode 12.2
  • 测试系统版本:iOS 14.2

异常表现

  • 开启编辑模式的SwiftUI List,完成行选中操作后,只要触发列表滚动,之前已选中的行就会自动取消选中,丢失选中标记
  • 滚动触发异常时,控制台输出固定断言信息:[Assert] Attempted to call -cellForRowAtIndexPath: on the table view while it was in the process of updating its visible cells, which is not allowed.
  • 问题效果配套三张演示素材,分别为初始状态下单元格选中的正常效果、列表向上滚动后的状态、列表向下滚动后的状态,可直观看到滚动后选中标记消失的现象

对照测试结论

该问题疑似为iOS 14.2的系统级bug:更换运行环境到iOS 14.1版本的模拟器后,相同代码下列表选中功能运行完全正常,无选中状态丢失、控制台断言报错的问题。

最小复现代码

import SwiftUI

struct ContentView: View {

    @State private var selectedRows = Set<String>()
    var items = ["item1", "item2", "item3", "item4","item5","item6","item7","item8","item9","item10","item11","item12","item13","item14","item15","item16","item17","item18","item19","item20","item21","item22"]
    
    var body: some View {
        
        NavigationView {
            List(selection: $selectedRows) {
                ForEach(items, id: \.self) { item in
                    Text(item)
                }
            }
            .listStyle(InsetGroupedListStyle())
            .environment(\.editMode, Binding.constant(.active))
            
        }
    }
}

内容的提问来源于stack exchange,提问作者Sphingobium

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.13 16:45:06