Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer to a cell outside of cellForRowAtIndexPath

I am trying to update a label in a cell outside of

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

by using

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! Cell

in another function. However, I keep getting errors. Therefore, I tried to refer to the cell by using let cell = Cell(), but I get the error unexpectedly found nil. Lastly, I tried

let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! Cell

that does not return an error, but the label cell.time.text is not updated.

like image 862
Dups Avatar asked Jun 05 '17 07:06

Dups


1 Answers

Use this:

tableView.cellForRow(at: IndexPath(row: row, section: section)) as! CustomCell

If your cell has properties like

@IBOutlet label: UILabel!

you can use this:

tableView.cellForRow(at: IndexPath(row: row, section: section)) as! CustomCell).label.text = "Test123" // smth like this

Hope it helps

like image 164
Vlad Pulichev Avatar answered Oct 19 '22 13:10

Vlad Pulichev