无法编译SwiftUI ForEach表

问题描述 投票:0回答:1

我正在尝试从动态数组制作表视图,但是编译器无法使用不相关的错误来构建它:编译器无法在合理的时间内对这个表达式进行类型检查;尝试将表达式分解为不同的子表达式

struct RecentView: View {
    @State var recentRooms: [[String: String]] = []

    @Binding var recentIsPressed: Bool
    @Binding var nextIsPressed: Bool
    @Binding var conferenceName: String

    let screenWidth = UIScreen.main.bounds.size.width

    var body: some View {
        if let recent = UserDefaults.standard.array(forKey: "recentRooms") as? [[String: String]] {
            recentRooms = recent
        }

        return VStack {

            ...

            VStack(alignment: .leading) {
                ForEach(0 ..< recentRooms.count) { i in
                    Button(action: { self.meetingSelected(name: self.recentRooms[i]["name"] ?? "") }) {
                        HStack {
                            ZStack {
                                Circle()
                                    .foregroundColor(Color("ProjectBlue"))
                                    .frame(width: 76, height: 76)
                                Text(self.recentRooms[i]["short_name"] ?? "")
                                    .font(.custom("Roboto-Bold", size: 36))
                                    .foregroundColor(Color("AlmostWhite"))
                            }
                            VStack(alignment: .leading) {
                                Text(self.recentRooms[i]["name"] ?? "")
                                    .font(.custom("OpenSans-Bold", size: 17))
                                    .foregroundColor(Color("AlmostWhite"))
                                Text(self.recentRooms[i]["date"] ?? "")
                                    .font(.custom("OpenSans-Regular", size: 17))
                                    .foregroundColor(Color("AlmostWhite"))
                                Text(self.recentRooms[i]["duration"] ?? "")
                                    .font(.custom("OpenSans-Regular", size: 17))
                                    .foregroundColor(Color("AlmostWhite"))
                            }
                        }
                    }
                    .frame(width: self.screenWidth, alignment: .leading)
                    .padding(.leading, 20)
                }
            }
        }
    }

    private func meetingSelected(name: String) {
        conferenceName = name
        nextIsPressed = true
        recentIsPressed = false
    }
}

关于ForEach部分有什么问题的任何建议?

foreach swiftui tableview
1个回答
1
投票

ForEach的此变体不应与动态内容一起使用。

ForEach(0 ..< recentRooms.count)

如果可以更改recentRooms变量,请改用此变量:

ForEach(recentRooms, id: \.self)
© www.soinside.com 2019 - 2024. All rights reserved.