35

I usually use viewDidAppear method to do some UI stuff on the view after it finished appearing and I used this method in various situations were it was very useful, however, I need to do some UI changes on a UITableViewCell after it finished appearing, is there any available method in the SDK that does a similar job like viewDidAppear but for UITableViewCell ?

p.s. willDisplayCell did not work in my case, I need something like didDisplayCell if it really exists.

JAHelia
  • 4,684
  • 14
  • 52
  • 102
  • Can you past some code which are using.. And what are you trying to achieve...I.e UI changes in the sense what kind of changes.. Can you please explain little more.. – Dilip Rajkumar May 27 '12 at 07:36
  • Take a look at this answer of this question: *Get notified when UITableView has finished asking for data?* http://stackoverflow.com/a/3060232/1028709 – Justin Boo May 27 '12 at 07:47
  • What is the reason you can't do things directly in init for your table cell subclass? and why won't `willDisplayCell` work for you? – Mattias Wadman May 27 '12 at 09:13
  • 2
    @Mattias - I have a complex situation which can't fit here to explain, but in a brief, after the cell is created, some frames of the cell's subviews are changed after the views have been created, so I need to fix these frames after the cell has appeared. – JAHelia May 27 '12 at 09:52

3 Answers3

36

The UITableViewDelegate has these functions:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath)

The cell itself does not have callbacks for this.

neoneye
  • 44,507
  • 23
  • 155
  • 143
12

If you need to respond to changes to the cell's frame and adjust the layout, you need to implement -layoutSubviews in the cell class. Remember to call [super layoutSubviews].

Mike Weller
  • 44,483
  • 14
  • 126
  • 148
  • 1
    The issue with layoutSubviews is that it is always called when a cell is dequeued. If you need to make a frame change only once when the cell is first setup you can do it in awakeFromNib. The issue with this is that you cannot get the adjusted bounds of the cell from autolayout in there, only in layoutSubviews. – Josh Bernfeld May 30 '15 at 21:09
4

The method viewDidLoad is related to UIViewController and its subclasses, so we cannot use this in UITableViewCell, which is a UIView subclass. The solution I used in one of my app is to override the layoutSubViews method of UITableViewCell, and I delayed the action for some time as below.

- (void)layoutSubViews
{
     [super layoutSubviews];

     [self performSelector:@selector(myMethod) withObject:nil afterDelay:1.0];
}
itsji10dra
  • 4,550
  • 3
  • 36
  • 55
Ganesh
  • 1,050
  • 1
  • 10
  • 28