应用程序在调用'tableView endUpdates'时崩溃

时间:2015-02-16 15:53:37

标签: ios objective-c uitableview didselectrowatindexpath

我目前正致力于在UITableViewCell内部实现inline UIDate picker

当我选择应该插入该单元格的正上方的单元格时,我能够显示和隐藏此选择器单元格,这是我期望的行为。但是,如果我在表格视图中选择任何其他单元格,则应用程序崩溃:

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

在查看accepted answer to this SO question之后,我添加了一个例外断点,我发现该应用在[tableView endUpdates];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];
}

那就是说,我不知道该怎么办。如果我注释掉[tableView endUpdates];的调用,则在选择其他单元格时应用程序不会崩溃,但是具有选择器视图的单元格将不会隐藏。有没有人有什么建议?谢谢!

编辑:以下是我的showTimePickerhideTimePicker代码:

- (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;
                     }];
}

编辑2:阅读this SO thread后,我认为问题可能出在我的numberOfRowsInSection方法上:

- (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;
    }
}

2 个答案:

答案 0 :(得分:2)

不确定这是否是崩溃的来源,但不建议多次调用beginUpdates你正在做什么 [tableView beginUpdates]; [self showTimePicker]; [tableView endUpdates]; 因为showTimePicker调用[self.tableView beginUpdates];

答案 1 :(得分:2)

问题出在你的didSelectRowAtIndexPath中,你调用了hide方法,即使它可能没有显示。将else子句变为else if,如下所示:

- (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 if (indexPath.section == TimeOfDaySection && indexPath.row == HourTimeZoneRow  && self.timePickerIsShowing == YES){
        [tableView beginUpdates];
        [self hideTimePicker];
        [tableView endUpdates];
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}