In my SwiftUI app, I have a data from an array. While scrolling through the list, the cells are being recycled, which is great. However, I'm puzzled because it seems that the data isn't being refetched as I scroll, which is contrary to what I expected.
I want to understand why the data isn't being refetched for recycled cells and if this is expected behavior.
class HistoryViewModel: ObservableObject {
@Published var filteredContacts: [HistoryData] = []
func updateFilteredContacts() {
filteredContacts = HistoryCallDataService().savedEntities
if self.searchText.isEmpty {
self.filteredContacts = filteredContacts
} else {
self.filteredContacts = filteredContacts.filter { contact in
contact.firstName?.localizedCaseInsensitiveContains(self.searchText) ?? false ||
contact.lastName?.localizedCaseInsensitiveContains(self.searchText) ?? false ||
contact.telephone?.localizedCaseInsensitiveContains(self.searchText) ?? false
}
}
}
The List:
List{
ForEach(vm.filteredContacts.reversed()) { item in
HStack{
VStack(alignment: .leading){
Text("\(item.firstName ?? "N/A") \(item.lastName ?? "N/A" )")
.fontWeight(.semibold)
Text("\(item.telephone ?? "N/A")")
.fontWeight(.medium)
.padding(.top,1)
}
Spacer()
VStack(alignment: .trailing){
Text("\(item.time ?? "N/A")")
.fontWeight(.medium)
Text("\(item.callHidden ? "Hidden" : "Normally ")")
.foregroundColor(item.callHidden ? Color.theme.red : Color.theme.black)
.fontWeight(.bold)
.padding(.top,1)
}
}
}
}
i attach image:
https://im.ezgif.com/tmp/ezgif-1-db6ebe2a2e.gif
[https://im.ezgif.com/tmp/ezgif-1-db6ebe2a2e.gif)