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.

Bigair
  • 1,452
  • 3
  • 15
  • 42

2 Answers2

11

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.

Vvk
  • 4,031
  • 29
  • 51
  • 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. – Bigair Feb 09 '16 at 05:09
  • 2
    @Bigair awakeFromNib is called only when first of cell load like in UIViewController – Vvk Feb 09 '16 at 05:16
  • @Bigair one anothyer way to load previus tableview cell is reload your tableview when it clicks on another **Tab** – Vvk Feb 09 '16 at 05:17
  • "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. – Bigair Feb 09 '16 at 05:39
  • I didn't realize tableView.reloadData() solution was on the answer, I just accepted the answer – Bigair Feb 09 '16 at 05:52
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
}
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
Hardik
  • 139
  • 1
  • 10
  • 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") – Zvi Oct 03 '16 at 14:59
  • @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) – Hardik Oct 03 '16 at 15:51
  • 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 working – Zvi Oct 03 '16 at 16:34