Reputation: 147
I'm working on swiftUI list and slightly struggled in swiftUI list. How to get a selected cell indexPath similar cellForRowAt in swift UITableViewDelegate protocol API.
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()
}
}
}
Upvotes: 0
Views: 819
Reputation: 769
You should be able to use the firstIndex(of:)
method on the contactRepo.contacts
array.
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.
Upvotes: 1