Dups
Dups

Reputation: 201

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.

Upvotes: 10

Views: 25745

Answers (7)

Ekramul Hoque
Ekramul Hoque

Reputation: 692

Swift 5.1 ++

Safe way to get custom cell refrence outside cellForRowAtIndexPath:

Here is Code :

let indexPath = IndexPath(row: 5, section: 0)
 
if let cell = self.yourTableView.cellForRow(at: indexPath) as? customCell {
       
     // Get all custom cell property refrence 
     // Do what ever you want ..
     
} 

Note:

You can get only visible cell refrence

Upvotes: 0

Gustavo Baiocchi Costa
Gustavo Baiocchi Costa

Reputation: 1448

You shouldn't be using touchUp inside targets. You might run into problems when scrolling fast and pressing buttons. You press one row button but when you release the finger that is another row button releasing the action. Also you might run into memory leaks. If you add a target you must remove it later! If you want to add targets it is better to use TouchDown event instead to avoid such problems. I personally use tapGestures. Then when the viewdisappear I find the cell which has a tapGesture and remove it for memory leaks issues.

Upvotes: 0

Harshal Valanda
Harshal Valanda

Reputation: 5451

Swift 3.0 & Swift 4

let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath)

Swift 2.3

let indexPath = NSIndexPath(forRow: 0, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath)

NOTE :- With help of above method you can only get visible cell of tableView except cell is nil

Upvotes: 31

Inder Jagdeo
Inder Jagdeo

Reputation: 229

If you want to use cell outside of the UITableView override method cellForRowAt indexPath then first of all, you need to fetch the indexPath of row and then using the indexPath of that row you can get the cell of UITableView.

Once, you get the cell then you can access their properties & methods outside of UITableView.

Try this:

In this code, i'm use a Custom Cell.

let indexPath = IndexPath.init(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! MembersCell

Upvotes: 9

Thilina Chamath Hewagama
Thilina Chamath Hewagama

Reputation: 9040

Step1:

update the data of the model

Step2:

reload indexPath

 let indexPathToRefresh = IndexPath(row: 0, section: 0)
 tableView.reloadRows(at: [indexPathToRefresh], with: .none)

Upvotes: 3

Vlad Pulichev
Vlad Pulichev

Reputation: 3272

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

Upvotes: 1

Michał Kwiecień
Michał Kwiecień

Reputation: 2874

Do not use dequeueReusableCellWithIdentifier outside the cellForRowAtIndexPath method.

You should use cellForRow(at:) https://developer.apple.com/reference/uikit/uitableview/1614983-cellforrow

Upvotes: 1

Related Questions