Rajan
Rajan

Reputation: 53

How do we use the invisible cells in uitableview swift?

I want to change my progress bar one by one in tableview. but when my use invisible cell in code, then it produces nil.

I use the tableview.cellforRowAt(IndexPath) in our code , Please resolve my problem

Upvotes: 0

Views: 817

Answers (1)

AamirR
AamirR

Reputation: 12208

UITableViewCell is reusable, you cannot update UI of cells that are not displayed (visible), they just do not exist or being reused for another cell.

Reusable means that cell's view (UITableViewCell) will be used for any other cell that might just got in to the tableView bounds, this happens when you delete a row, or when you scroll away and the cell gets out of tableView bounds etc...

So, its not a problem, but by design.

BTW, you are using the correct method tableView.cellForRow(at: IndexPath), it will return a cell only if one is displayed.

You should instead update your data source and keep the progress value, when the cell next time being displayed it will show the value from data source, like so:

struct CellData {
    var progress: Float
}

var dataSource: [CellData]

Now when you need to update the progress of cells, you can just update dataSource, like so

dataSource[index].progress = value

Then you call either call UITableView.realoadData() to refresh the UI of all cells that are visible, or you can update only that particular index, like so:

if let cell = self.tableView.cellForRow(at: IndexPath(row: index, section: 0)) as? CustomCell {
    cell.progressView.setProgress(value, animated: true)
}

Upvotes: 3

Related Questions