Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift, how do I iterate through every cell in a UITableView and then get its properties?

Tags:

ios

swift

I have a generic UITableView and I want to go through every visible cell.

How can I do this in swift?

like image 700
TIMEX Avatar asked Jun 29 '15 15:06

TIMEX


4 Answers

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?

like image 120
Rachel Harvey Avatar answered Nov 07 '22 01:11

Rachel Harvey


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.

like image 44
Sven Avatar answered Nov 07 '22 00:11

Sven


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)
}
like image 6
sylvanaar Avatar answered Nov 07 '22 02:11

sylvanaar


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

like image 4
Russell Austin Avatar answered Nov 07 '22 02:11

Russell Austin