Swift SwiftUI-scrollview错误?错误:编译器无法在合理的时间对该表达式进行类型检查;尝试分手

时间:2020-09-01 14:43:14

标签: swift swiftui

我真的在这里迷路了。这是我的错误还是XCode的错误?这是关于带有List的ContentView的,用户可以按一个按钮跳转到带有相应字母的名称。我遇到两个问题,一个是上面的错误,第二个是,如果ClientData更改了列表顶部的索引按钮,则应该对其进行更新。首先,我想在.onappear中执行此操作,但随后,像现在一样,我只获得名称的第一字母,这些字母在屏幕上可见。因此,当我对Client Array排序以收集firstLetter时,我喜欢构建列表。

import SwiftUI
import PlaygroundSupport
import Combine

struct ClientModel : Identifiable, Hashable {
    var id : Int
    var firstName : String
    var lastName : String
}

class ClientData : ObservableObject   {
 @Published var clients : [ClientModel] = [
    ClientModel(id: 1, firstName: "jan", lastName: "Pieters"),
    ClientModel(id: 2, firstName: "kees", lastName: "Jansen"),
    ClientModel(id: 3, firstName: "jaap", lastName: "Jansen"),
    ClientModel(id: 66, firstName: "koos", lastName: "Werkeloos")
 ]
}


struct ContentView: View {
 @ObservedObject var clients = ClientData()
 @State private var indexDict : Dictionary<String, Int> = [:]


 var body: some View {
    ScrollViewReader { scrollProxy in
        VStack {
            HStack {
                ForEach ( indexDict.sorted(by: { (lhs, rhs) -> Bool in
                                            lhs.key < rhs.key}), id: \.key ) { key, value in
                    Button {
                        withAnimation {
                            scrollProxy.scrollTo(value, anchor: .top)
                        }
                    }.buttonStyle(SectionIndexButtonStyle())
                }
            }
            ScrollView {
                LazyVStack {
                    ForEach( clients.clients.sorted(by: { (lhs, rhs) -> Bool in
                                                        lhs.lastName! < rhs.lastName!}), id: \.id ) { client in
                        buildIndex(proxy: scrollProxy, item: client)
                        RowView(text: (client.lastName! + ", " + client.firstName! + ", " + "\(client.id)" ) as String )
                            .onAppear( print(client.lastName!) )
                            .id( client.id )
                    }
                }
            }
        }
    }
    .navigationTitle("Clients")
 }

 func buildIndex(proxy: ScrollViewProxy, item: ClientModel) {
    let firstLetter = item.lastName.trimmingCharacters(in: .whitespaces).prefix(1).uppercased()
    print("firstLetter : \(firstLetter)")

    if indexDict.keys.contains(firstLetter) {
        if indexDict[firstLetter] != nil {
            indexDict[firstLetter] = item.id
            print("did contain the letter but empty id")
        } else {
            print(indexDict[firstLetter] ?? "??")
            print("does contain the letter and id")
        }
    } else {
        print("Not found")
        indexDict[firstLetter] = item.id
        print("does contain now the letter and id")
    }
 }
}

struct MySubView: View {
 @Binding var contentViewConfig : ContentViewConfig
 var body : some View {
    // ...
    Text("press me")

 }
}

struct RowView: View {
 let text: String

 var body: some View{
    Text(text)
        .padding()
        .frame(maxWidth: .infinity, alignment: .leading )
 }
}

struct SectionIndexButtonStyle: ButtonStyle {
 func makeBody(configuration: Self.Configuration) -> some View {
    configuration.label
        .foregroundColor(configuration.isPressed ? Color.gray.opacity(0.2) : Color.accentColor.opacity(0.1))
        .background(configuration.isPressed ? Color.white : Color.gray.opacity(0.1))
        .cornerRadius(15.0)
        .overlay(RoundedRectangle(cornerRadius: 15.0) .stroke(lineWidth: 1.0).foregroundColor(.accentColor))
        .padding(0)
 }
}

PlaygroundPage.current.liveView = UIHostingController(rootView:
                                                    ContentView())

我确实注释了几个部分,以检查在哪里可以找到该crillpit,但仍然遇到相同的错误。有什么想法,我怎么能理解这个问题?

0 个答案:

没有答案