ViewWillAppear on UITableViewCell?
Asked Answered
D

2

9

Is there any way to perform something like ViewWillAppear on UITableViewCell?

UITableViewDelegate methods (like willDisplayCell) only works when cells appear by scrolling.

In my situation, I need to detect cell appearance in a situation like user moves to another Tab and gets back to the UITableView.

I was able to solve the my problem using indexPathsForVisibleRows method but this doesn't seem to be a smart way of doing it.

Dichloride answered 9/2, 2016 at 4:48 Comment(1)
What do you want to perform when user came back from another tab ?Collazo
C
12

Yes. you have to use awakeFromNib meyhod inside the m file. this method is always call first.

-(void)awakeFromNib{

}

if visible cell not load then you have to reload your tableview.

 tableView.reloadData()

Hope it helps you.

Cymbal answered 9/2, 2016 at 4:52 Comment(5)
Thanks for the answer but awakeFronNib is not called in a case like a user moves to another Tab and gets back to the UITableView.Dichloride
@Dichloride awakeFromNib is called only when first of cell load like in UIViewControllerCymbal
@Dichloride one anothyer way to load previus tableview cell is reload your tableview when it clicks on another TabCymbal
"calling tableView.reloadData() in the UITableView viewWillAppear" as Vvk Aghera suggested was a perfect solution. This should be an accepted answer. Great and very smart solution, thanks a lot.Dichloride
I didn't realize tableView.reloadData() solution was on the answer, I just accepted the answerDichloride
L
7

I'm not sure whether this will work for you as you want to do something on tab change as well otherwise it will work.

I know I am answering too late, but I am answering to help others who view this post.

So you can try this: (Objective-C)

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell  
                                         forRowAtIndexPath:(NSIndexPath *)indexPath {
    // Do what ever you want
}

(Swift)

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    // Do what ever you want
}
Lavellelaven answered 19/7, 2016 at 17:50 Comment(3)
I tried to use this method (swift) but I got an error to remove "override" -"Method does not override any method from its superclass". When I remove the "override" the app compiles but the method is not called, although other delegate methods are called (such as "didSelectRowAtIndexPath")Alon
@Zvi, Thanks. Updated my answer. Can you try following method instead of willDisplayCell and let me know your result: func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)Lavellelaven
First need to use indexPath: NSIndexPath (mistake even in Apple documentation). Second, I do not know what I did but now it works, using willDisplayCell - willDisplay does still not workingAlon

© 2022 - 2024 — McMap. All rights reserved.