[英]How to find a selected List cell indexPath in swiftUI
I'm working on swiftUI list and slightly struggled in swiftUI list.我正在处理 swiftUI 列表,但在 swiftUI 列表中略有挣扎。 How to get a selected cell indexPath similar cellForRowAt in swift UITableViewDelegate protocol API.
如何在swift UITableViewDelegate协议 API 中获取类似于cellForRowAt的选定单元格 indexPath。
My list structure is:我的列表结构是:
struct ContactList: View {
@ObservedObject var contactRepo = ContactRepository()
var body: some View {
NavigationView {
List(contactRepo.contacts){ contact in
ContactCell(data: contact)
}
.navigationTitle("Contacts")
}
}
}
List cell structure is:列表单元结构是:
struct ContactCell: View {
var contactData: Contact
var body: some View {
HStack {
Image(systemName: "person.fill")
.data(urlStr: contactData.profileUrl)
.frame(width: 60, height: 60)
.border(separatorColor, width: 1)
.cornerRadius(5)
.padding(10)
VStack(alignment: .leading) {
Text(contactData.name)
.font(.system(size: 20, weight: .semibold, design: .default))
.foregroundColor(appTextColor)
Text(contactData.role)
.font(.system(size: 17, weight: .thin, design: .default))
.foregroundColor(appTextColor)
}
Spacer()
}
}
}
You should be able to use the firstIndex(of:)
method on the contactRepo.contacts
array.您应该能够在
contactRepo.contacts
数组上使用firstIndex(of:)
方法。
let contact: Contact = /* ... */
let index = contactRepo.contacts.firstIndex(of: contact)
If you want to insert something into the list, you can just modify the contactRepo.contacts
array.如果你想在列表中插入一些东西,你可以修改
contactRepo.contacts
数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.