Search code examples
iosobjective-cuitableviewcell

Is there any method like tableViewWillEndDisplayingCell in iOS for UITableViewDelegate?


I would like to fade cell in table view in my iPhone application before it disappears. When it is about to appear I use

- (void) tableView: (UITableView *) tableView
   willDisplayCell: (UITableViewCell *) cell
 forRowAtIndexPath: (NSIndexPath *) indexPath

to change alpha from zero to 1 with 0.3 sec animation.

I would like to to the same but from 1 to zero when it is about to disappear. Is it any method similar to the one above or how else can I do that?

I don't want to use standard iOS UITableViewRowAnimationFade because I don't want any other transitions while reloading table.

EDIT: I am reloading content of the table view. Let say that I have a button which I click and it reloads data in my table. That is when I want to fade out existing cells and fade in new ones.

The controller I am using is a basic UIViewController implementing UITableViewDataSource UITableViewDelegate UIScrollViewDelegate protocols.


Solution

  • I found a solution.

    Just before calling [self.tableView reloadData] I am animating all visible cells to fade out. I get all visible cells and iterate over them by

    for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows])
    {
        // here I animate cells to fade out
    }
    

    (thanks @AlexKoren for showing me the way to iterate over visible cells).

    Because I was using [self.tableView reloadData] without any additional iOS built-in animation the cells were just disappearing. So now they are fading first and then my new cells are fading in with animation defined in

    - (void) tableView: (UITableView *) tableView
       willDisplayCell: (UITableViewCell *) cell
     forRowAtIndexPath: (NSIndexPath *) indexPath
    

    Important: I am reloading table view after animation is finished. If I called [self.tableView reloadData] just after iterating over cells it would reload table before animation was displayed. Because I know how long it takes to finish animation I use

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void)
                    {
                        [self.tableView reloadData];
                    });
    

    with delay calculated before.