首页 > 解决方案 > 表单中的 SwiftUI Pickers 出错:UITableView 被告知在不位于视图层次结构中的情况下布局其可见单元格和其他内容

问题描述

在表单中使用 SwiftUI 选择器出现此错误。当用户在显示的选项表中选择一个选项时会发生错误。

该错误仅发生在我运行 iOS 13.1 的物理设备 (iPhone 8) 上:它不会在模拟器中的 iOS 13.1 上发生。

这里有一些显示问题的屏幕:

表单中的 SwiftUI 选择器 Bug Screenshots

import SwiftUI

struct ContentView: View {
    @State private var selectedLanguage: Language = .en
    var body: some View {
        NavigationView{
            Form {
                List{
                    Picker("Choose language", selection: $selectedLanguage){
                        ForEach(Language.allCases, id: \.self) { lang in
                            HStack{
                                Text(lang.rawValue)
                            }.tag(lang)
                        }
                    }.pickerStyle(DefaultPickerStyle())
                }
            }
        }
    }
}

enum Language: String, CaseIterable {

    case en = " English"
    case ch = " Chinees"
    case ru = " Russian"
    case es = " Spain"
    case se = " Sweden"

    var toString: String {
      return self.rawValue
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

我希望复选标记在 iPhone 设备上正常工作,但我在设备上看不到任何复选标记并收到此错误:

UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window.

标签: uitableviewlayoutswiftuipicker

解决方案


推荐阅读