ios - SwiftUI 列表仅使用给定数组的第一项

标签 ios swiftui contacts cncontactstore xcode11.3

我在下面有一个简单的 View 来显示用户的所有联系人:

struct AllContactsView: View {
    static let withState: some View = AllContactsView().environmentObject(AllContactsProvider())
    @EnvironmentObject var contactsProvider: AllContactsProvider
    let title: UserListType = .selectInvitees

    var body: some View {
        List(self.contactsProvider.invitees) { invite in
            self.row(for: invite)
        }
        .navigationBarTitle(Text(self.title.rawValue))
        .onAppear(perform: self.contactsProvider.fetch)
    }

    func row(for invite: Invitee) -> some View {
        // everything below is only printed out once!
        print(self.contactsProvider.invitees.prettyPrinted) // shows correct array of contacts
        print(type(of: self.contactsProvider.invitees)) // this is indeed an array
        print(invite) // prints out the first item in the array (which is expected on first pass)
        return UserRow(invitee: invite)
    }
}

我正在操作 CNContacts 的数组,我得到这样一个被邀请者数组,这是我试图在我的列表中显示的内容:

self?.invitees = contacts.asSimpleContacts.map({ $0.asUser.asInvitee })

使用下面的支持功能和扩展:

// Contact Value simplified so we can pass it around as a value type.
public struct SimpleContact: Hashable, Codable {
    let firstName: String
    let lastName: String
    let emails: [String]
    let phoneNumbers: [PhoneNumber]

    var fullName: String { "\(self.firstName) \(self.lastName)" }

    var asUser: User {
        User(
            id: Constants.unsavedID,
            username: self.fullName,
            picURL: "al",
            email: self.emails.first ?? "",
            phone: self.phoneNumbers.first ?? "",
            created: Date().timeIntervalSince1970
        )
    }
}


extension CNContact {
    /// Returns the `SimpleContact` representation of `self`
    var asSimpleContact: SimpleContact {
        SimpleContact(
            firstName: self.givenName,
            lastName: self.familyName,
            emails: self.emailAddresses.map({ String($0.value) }),
            phoneNumbers: self.phoneNumbers.map({ Authentication.sanitize(phoneNo: $0.value.stringValue) })
        )
    }
}

extension Array where Element == CNContact {
    /// Returns the `SimpleContact` mapping of `self`
    var asSimpleContacts: [SimpleContact] { self.map({ $0.asSimpleContact }) }
}

public struct User: Hashable, Codable, Identifiable {
    public let id: String
    let username: String
    let picURL: String
    let email: String
    let phone: String
    let created: Double

    var asInvitee: Invitee { Invitee(user: self, isGoing: false) }
}

按照 self.contactsProvider.fetch() 的预期,联系人将填充到 self.contactsProvider.invitees 中。但是,SwiftUI 显示的是 self.contactsProvider.invitees.countself.contactsProvider.invitees.first 实例,而不是每个联系人。我将下面的方法与其他在线示例进行了比较,但似乎无法找到我出错的地方。我确定问题出在联系人操作上 - 当我提供一组模拟的受邀者时,一切都按预期工作,尽管在没有模拟的情况下按预期编译和运行,并且打印和调试没有显示任何内容。

如有任何帮助,我们将不胜感激。

最佳答案

我刚遇到这个问题,回答得更清楚一点:

SwiftUI 列表中的每一行都应使用唯一 ID 生成。

如果您使用 List() 函数创建 View ,请确保您指定了一个 id

struct MenuView: View {
var body: some View {
    VStack {
        Section(header: MenuSectionHeader()) {
            //item.sku is the unique identifier for the row
            List(Menu.allItems, id:\.sku) { item in 
                MenuItemRow(item)
            }
        }
    }
}

关于ios - SwiftUI 列表仅使用给定数组的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60581714/

相关文章:

ios - 如何获取 UITableView 中的所有单元格

iphone - iOS 5 : How to implement a custom (image) back button

ios - SwiftUI中的高级动画

ios - 旋转设备后动画点会重新定位

android - 接收联系人更改(添加、删除和更改)

ios - UISplitViewController 中的 masterViewController 未显示

objective-c - 获取 iOS 支持的货币列表

swift - 列表选择为 Set<String> - 如何使用?

ruby - 使用 Omniauth 在 Ruby 中请求 Google 联系人

android - 如何在 ListView 中显示电话联系人