Question

I have a table view with static cells. Clicking on a cell toggles visibility (changes cell height) of picker view cell, associated with this cell. A tag property of clicked cell maintains state of a picker cell. Animations are triggered with a sequence of beginUpdates, endUpdates methods. This is an easy way to animate visibility of picker cells (without managing cell insertions and deletions).

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat defaultHeight = [super tableView:tableView heightForRowAtIndexPath:indexPath];

    switch (indexPath.row) {
        case RowIndexDayTimePicker:
            return self.dayTimeCell.tag > 0 ? defaultHeight : 0;

        case RowIndexPrivacyPicker:
            return self.privacyCell.tag > 0 ? defaultHeight : 0;

        default:
            return defaultHeight;
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case RowIndexDayTime:
            self.dayTimeCell.tag *= -1;
            self.privacyCell.tag = -1;
            break;

        case RowIndexPrivacy:
            self.dayTimeCell.tag = -1;
            self.privacyCell.tag *= -1;
            break;

        default:
            break;
    }

    [tableView beginUpdates];
    [tableView endUpdates];
}

Animations work as expected, until I set table view separator property to None in order to use custom separator. Compare two screenshots (animated gif):

Table view Table view w/o separators

I have no idea how to fix it. I use autolayout, picker views are aligned to superview with zero margin from each side. Tested under iOS 7 Simulator and iPos Touch. Storyboard screenshot:

Storyboard

Was it helpful?

Solution

Seems like a UITableView bug.

How about:

self.tableView.separatorColor = [UIColor whiteColor]; //or clearColor?

Or:

self.tableView.separatorInset = UIEdgeInsetsMake(0, CGRectGetWidth(self.view.bounds), 0, 0);

(The goal being to hide instead of remove the separator.)

OTHER TIPS

I played a lot with height changes animation for table cells, unfortunately there are some problems with all methods I know.

Try to use this method of UITableView:

[UITableView reloadRowsAtIndexPaths:rowsToReload  withRowAnimation: UITableViewRowAnimationNone];

Also try to change UITableViewRowAnimationNone to UITableViewRowAnimationAutomatic (I found that UITableViewRowAnimationNone leads to incorrect animation on iPhone/iPod)

For my project I ended with animated [tableView reloadData] method:

[UIView transitionWithView: self.tableView duration: 0.3 options: UIViewAnimationOptionTransitionCrossDissolve animations:^{
    [self.tableView reloadData];
} completion: nil];

I think your table is reloading twice. Remove following code, it will work.

[tableView beginUpdates]; 
[tableView endUpdates];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top