viewDidAppear for UITableViewCell
Asked Answered
I

4

39

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.

Iamb answered 27/5, 2012 at 7:10 Comment(4)
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..Jenness
Take a look at this answer of this question: Get notified when UITableView has finished asking for data? https://mcmap.net/q/23855/-get-notified-when-uitableview-has-finished-asking-for-dataApetalous
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?Erinaceous
@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.Iamb
G
40

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.

Gale answered 7/5, 2016 at 10:52 Comment(0)
W
14

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].

Wriggly answered 27/7, 2012 at 12:1 Comment(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.Herbarium
C
5

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];
}
Chagres answered 27/5, 2012 at 9:7 Comment(7)
using a delay in your UI config? Seems like horrible practice to me no?Mort
Horrible practice indeed.Whoreson
Not necessarily. If he is looking to mitigate a performance impact for a UIScrollView this may be a possible solution.Herbarium
For anyone who references this - the method name is layoutSubviews (note the case), and you should also call [super layoutSubviews]Barkley
12 up votes to the "horrible practice" comment but no better suggestion?Residential
Any solution? I don't find event for UITableviewCell did view, so I successfully do this afterDelay 0.1 .Pereyra
in-line attenuatorCollettecolletti
B
1

You can use didMoveToSuperview and do your cell updates there

override func didMoveToSuperview() {
    super.didMoveToSuperview()

    if superview != nil {
        // Update the cell
    }
}

In case you need something faster, use willDisplayCell

func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    // Update the cell
}
Byandby answered 23/6, 2021 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.