17

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

How can I do this in swift?

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 3
    This question doesn't make any sense: Cells are pooled and reused. Generally, if you don't see it on a screen it doesn't exist. Instead what you would want to do is iterate through everything in your underlying data structure. Can you elaborate on what you're trying to do please? – pbush25 Jun 29 '15 at 15:49
  • He said **visible cells**, you can do that, perhaps he edited his question – sylvanaar Jun 29 '15 at 16:10
  • No perhaps about it -- you can read the edit. The original question was explicitly *all* cells, visible and non-visible. – Brad Brighton Jun 29 '15 at 17:05
  • The original question makes perfect sense to me. Even if not visible, the cells can still exist. These can for example be subscribing to data, and you would like to turn that subscription off when they are no longer visible. Doing this in deinit() is not good enough, because they can still be alive in the reuse pool. – Hans Terje Bakke Mar 27 '19 at 18:55

7 Answers7

34

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?

Community
  • 1
  • 1
Rachel Harvey
  • 1,719
  • 2
  • 15
  • 23
8

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.

Sven
  • 22,475
  • 4
  • 52
  • 71
6

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)
}
sylvanaar
  • 8,096
  • 37
  • 59
  • How come "cell" does not contain the custom properties? (I subclassed UITableViewCell) – TIMEX Jun 30 '15 at 05:07
  • You have to downcast it to your cell type using `as`. If you are sure about the type you can say `cell as! YourCellType` – sylvanaar Jun 30 '15 at 15:20
4

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

Community
  • 1
  • 1
Russell Austin
  • 409
  • 3
  • 7
2

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
}
Chris Deck
  • 146
  • 2
  • 9
1

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.

Mor4eza
  • 265
  • 3
  • 12
0

[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.

Brad Brighton
  • 2,179
  • 1
  • 13
  • 15