App crashes on call to 'tableView endUpdates'

narner picture narner · Feb 16, 2015 · Viewed 10.3k times · Source

I'm currently working on an implementation of an inline UIDate picker inside of a UITableViewCell.

I'm able to show and hide this picker cell when I select the cell directly above where that cell should be inserted, which is the behavior that I expect. However, the app crashes if I select any other cells in the table view:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:1582

After looking at the accepted answer to this SO question, I added an exception breakpoint, and I've found out that the app is crashing at the call to [tableView endUpdates]; in didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    // Check to see if the "Only Alert From" row was selected. The cell with the picker should be below this one.
    if (indexPath.section == TimeOfDaySection && indexPath.row == HourTimeZoneRow  && self.timePickerIsShowing == NO){

        [tableView beginUpdates];
        [self showTimePicker];
        [tableView endUpdates];

    } else{
        [tableView beginUpdates];
        [self hideTimePicker];
        [tableView endUpdates];
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

That said, I'm not sure how to proceed. If I comment out the call to [tableView endUpdates];, the app won't crash when other cells are selected, BUT the cell with the picker view won't hide. Does anyone have any suggestions? Thank you!

EDIT: Below is my code for showTimePicker and hideTimePicker:

- (void)showTimePicker
{
    self.timePickerIsShowing = YES;
    self.timePicker.hidden = NO;

    //Create the index path where we insert the cell with the picker
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:HourTimeZoneRow + 1 inSection:TimeOfDaySection];

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];

    self.timePicker.alpha = 0.0f;
    [UIView animateWithDuration:0.25 animations:^{
        self.timePicker.alpha = 1.0f;
        //This is the row where the picker cell should be inserted
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:HourTimeZoneRow + 1 inSection:TimeOfDaySection]] withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView reloadData];
    }];
}

- (void)hideTimePicker {
    self.timePickerIsShowing = NO;
    self.timePicker.hidden = YES;

    //Create the index path where we delete the cell with the picker
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:HourTimeZoneRow + 1 inSection:TimeOfDaySection];
    [self.tableView beginUpdates];
    //Delete the picker row
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
    [UIView animateWithDuration:0.25
                     animations:^{
                         self.timePicker.alpha = 0.0f;
                     }
                     completion:^(BOOL finished){
                         self.timePicker.hidden = YES;
                     }];
}

EDIT 2: After reading this SO thread, I believe the problem may be with my numberOfRowsInSection method:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case NotificationsSection:
            return TotalPreferencesRows;
            break;
        case RedZoneSection:
            return TotalRedZoneRows;
            break;
        case TimeOfDaySection:
            if (self.timePickerIsShowing) {
                return TotalTimeOfDayRows + 1;
            }
//            else if (self.timePickerIsShowing == NO){
//                return TotalTimeOfDayRows;
//            }
            else{
                return TotalTimeOfDayRows;
            }
            return TotalTimeOfDayRows;
            break;
        default:
            return 0;
            break;
    }
}

Answer

Micky picture Micky · Feb 16, 2015

Not sure if this is the source of the crash, but it's not recommended to call beginUpdates multiple times which you're doing in
[tableView beginUpdates]; [self showTimePicker]; [tableView endUpdates]; because showTimePicker calls [self.tableView beginUpdates];