如果删除第一部分 - 应用程序崩溃

时间:2014-07-25 04:26:02

标签: ios objective-c uitableview

如果我删除了我的部分中的最后一个单元格,我需要删除部分

如果我删除以结束开头的部分 - 它的工作完美,但如果我删除我的第一部分 - 应用程序崩溃

和我的上一节 - 添加按钮

会发生什么?

我的代码

- (void)deleteExerciseFromCustomTable:(NSIndexPath *)indexPath {
    [[_exercises objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];

    [_previewTable beginUpdates];
    [_previewTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    [_previewTable endUpdates];

    if ([[_exercises objectAtIndex:indexPath.section] count] == 0) {
        [_group removeObjectAtIndex:indexPath.section];
        [_previewTable beginUpdates];
        [_previewTable deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];
        [_previewTable endUpdates];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == [_group count]) {
        return 1;
    }else{
        return [[_exercises objectAtIndex:section] count];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [_group count] + 1;
}

1 个答案:

答案 0 :(得分:0)

我解决了我的问题

 - (void)deleteExerciseFromCustomTable:(NSIndexPath *)indexPath {

    if ([[_exercises objectAtIndex:indexPath.section] count] == 1) {
        [_group removeObjectAtIndex:indexPath.section];
        [_exercises removeObjectAtIndex:indexPath.section];        
        [_previewTable beginUpdates];
        [_previewTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        [_previewTable deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight];
        [_previewTable endUpdates];

    }else{
        [[_exercises objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];            
        [_previewTable beginUpdates];
        [_previewTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];        
        [_previewTable endUpdates];        

    }
}