I have a List in SwiftUI but the problem is that I have a search box.
My code is like this:
List {
ForEach(items.filter {
self.searchText.isEmpty ? true : $0.term!.contains(self.searchText)
}, id: \.self) { item in
Text(item.term!)
}
.onDelete(perform: deleteItems)
.onTapGesture(count: 1, perform: {
selectedItem = index // 1
})
}
I need to get the index of the selected item from that list, when the item is selected.
See the line //1? How do I get the index
there.
The solution by Aperi is not good, because the only elements tappable will be the texts, not the cells.
The correct answer is to wrap the
Text
element as a view of a Button, like shown on this video...