Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when UITableViewCell did load

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?

like image 651
Tometoyou Avatar asked Feb 22 '16 16:02

Tometoyou


1 Answers

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
    }
}
like image 182
Craig Siemens Avatar answered Sep 19 '22 01:09

Craig Siemens