I want to know which cells are not visible anymore of a UITableView so I can delete their data from the DataModel class where they are stored.
This is what I have tried but does not work. I guess because indexPathsForVisibleRows can't be used as publisher.
func addSubscriberToVisibleCells(){
var publisher = self.table_view.indexPathsForVisibleRows.publisher.receive(on: DispatchQueue.main)
print("Publisher added")
publisher.sink { pathsVisible in
guard pathsVisible != nil else{return}
self.lastIndexPathRows.forEach { lastIndex,value in
if pathsVisible.firstIndex(of: lastIndex) == nil {
self.postData.deleteDataForIndex(lastIndex)
self.lastIndexPathRows.removeValue(forKey: lastIndex)
}
}
pathsVisible.map { visibleIndexPath in
self.lastIndexPathRows[visibleIndexPath] = 1
}
}
}
self.lastIndexPathRows is a dictionary to keep track of the latest visible cells.
CodePudding user response:
Implement tableView(_:didEndDisplaying:forRowAt:)
in your table view delegate.