TIMEX
TIMEX

Reputation: 271704

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

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

Answers (7)

Mor4eza
Mor4eza

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

Chris Deck
Chris Deck

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

Brad Brighton
Brad Brighton

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

Rachel Harvey
Rachel Harvey

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

sylvanaar
sylvanaar

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

Russell Austin
Russell Austin

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

anon
anon

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

Related Questions