I need to be able to detect when my custom UITableViewCell
has been loaded on screen, so that I can programmatically do stuff to the view inside it. The only code I could get working was implementing layoutSubviews()
inside the custom UITableViewCell
, but this is bad practice because layoutSubviews()
is called more than one time. Also, putting my code in awakeFromNib()
doesn't work because I need to call a delegate property, which hasn't been set at that point. How can I achieve this?
There are two different ways you could do it.
In your UITableViewDelegate
you can implement the delegate method that is called when a cell is about to be displayed.
func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
// Update the cell
}
Or in your UITableViewCell
subclass you could implement didMoveToSuperView
.
override func didMoveToSuperview() {
super.didMoveToSuperview()
if superview != nil {
// Update the cell
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With