Reputation: 201
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
Reputation: 692
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 ..
}
You can get only visible cell refrence
Upvotes: 0
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
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
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
Reputation: 9040
update the data of the model
reload indexPath
let indexPathToRefresh = IndexPath(row: 0, section: 0)
tableView.reloadRows(at: [indexPathToRefresh], with: .none)
Upvotes: 3
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
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