tableView:为不应删除的行调用了commitEditingStyle

时间:2017-04-10 07:13:26

标签: ios objective-c uitableview

最近,我在commitEditingStyle的行上遇到了崩溃:forRowAtIndexPath(我的tableView只有1节)

HttpRuntime.Cache

崩溃是commitEditingStyle中的最后一行,而且 来自崩溃日志

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ((indexPath.row > 2 && [_answer.agreeUsers count] == 0) || (indexPath.row > 3 && [_answer.agreeUsers count] > 0)) {
        iKMAppDelegate *appDelegate = (iKMAppDelegate *)[UIApplication sharedApplication].delegate;
        NSUInteger step = 3;
        if ([_answer.agreeUsers count] > 0) {
            step = 4;
        }
        Comment *comment = [_comments objectAtIndex:(indexPath.row - step)];
        return [appDelegate.appDataObject.currentUser isEqualToString:comment.createdBy];
    } else {
        return NO;
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        NSUInteger step = 3;
        if ([_answer.agreeUsers count] > 0) {
            step = 4;
        }

        Comment *currentSelectedComment = [_comments objectAtIndex:(indexPath.row - step)];
}

似乎commitEditingStyle得到了呼吁indexPath(0,0),(因为18446744073709551612是-4无符号整数,所以indexPath.row必须为0),但它显然,我只能在canEditRowAtIndexPath返回YES时, indexPath.row大于3或4,因此对于row = 0,canEditRowAtIndexPath总是返回NO。如何为commitEditingStyle调用index = 0的indexPath?

0 个答案:

没有答案