I have a generic UITableView and I want to go through every visible cell.
How can I do this in swift?
I'm currently using this in one of my projects:
let cells = self.tableView.visibleCells as! Array<UITableViewCell>
for cell in cells {
// look at data
}
Here's another answer, but it's in objective-C: How can I loop through UITableView's cells?
You can't. UITableView doesn't keep any cells that are not visible. As soon as a cell moves completely off-screen it is removed and added to the reuse queue.
You said visible cells. You have the ability to iterate through those. Here is some sample code, you just need to get your own reference to the table.
let table = UITableView()
for cell in table.visibleCells() {
print(cell)
}
Your table has a data source, often times the data source is an array of objects. The data source determines how many items are in the table. You could iterate over that data source and use cellForRowAtIndex path to get the cell object.
The answer to this question has some information on cellForRowAtIndexPath
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With