Reputation: 271704
I have a generic UITableView and I want to go through every visible cell.
How can I do this in swift?
Upvotes: 17
Views: 29988
Reputation: 305
After struggling for a day, finally i've found the best solution!
if you want access to cells that are not visible in a UITableView
you should use scrollViewDidScroll
and iterate cells.
at first set tableView delegate:
creditCardTableView.delegate = self
then:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let cells = self.creditCardTableView.visibleCells as? [CreditCardLoanCell] else {
return
}
cells.forEach { cell in
cell.delegate = self
}
}
*Note: this is useful when you are not able or don't want to use cellForRowAt
function.
Upvotes: 1
Reputation: 146
I created an array of cells and each time I create a cell in cellForRowAt I added it to the array, if it is not already in there. Code is below.
var cells:[ProfileTableViewCell]! //initialize array at class level
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//your code to customize cell
if(!cells.contains(cell)){
self.cells.append(cell)
}
return cell
}
Then later you can loop through this array....
for cell in cells {
//do something with cell
}
Upvotes: 2
Reputation: 2184
[NOTE: This answer was for the original question which explicitly included non-visible cells. Even after the mod though, unless the intent is for post-configuration presentation-specific cell properties, it's still appropriate to refer back to the datasource.]
Properly configured cells only have "properties" when they are visible, as they get reused as necessary.
Walk the elements of your datasource (the details of the iteration will vary depending on what type of structure you're using as a datasource) in order to determine the attributes you're looking for.
If you feel you have a valid reason to inspect the cells themselves anyway, you will need to keep a separate list of them from cellForRowAtIndexPath
time; whenever a new cell has been created, add it to the list.
Upvotes: 0
Reputation: 1789
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?
Upvotes: 35
Reputation: 8216
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)
}
Upvotes: 6
Reputation: 409
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
Upvotes: 4
Reputation:
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.
Upvotes: 8