tableview在iphone中编辑崩溃应用程序?

时间:2010-12-09 07:53:00

标签: iphone objective-c uitableview row editing

在app中我有自定义tableview(UITableView *tableView) uiviewcontroller&的子类。在tableview编辑中显示完美,但当我删除行

- (void)tableView:(UITableView *)tableView1 commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        //[appDelegate deleteItemAtIndexPath:indexPath];
        [self testing:indexPath];
        [tableView deleteRowsAtIndexPaths:
         [NSArray arrayWithObject:indexPath] 
                     withRowAnimation:UITableViewRowAnimationFade];//in this line app crash 
        NSLog(@"delete row");
        [tableView reloadData];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        //nothing to do item already added
    }   
} 
当断点进入该行

时,此方法中的

[tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

我的应用程序崩溃了我做的事情。

- (void)testing:(NSIndexPath *)path {

Player *doc = [[Player alloc]init];
NSMutableArray* reversedArray = [[NSMutableArray alloc] initWithArray:[[[[NSArray alloc] initWithArray: _data] reverseObjectEnumerator] allObjects]];

doc = [reversedArray objectAtIndex:path.row];
NSLog(@"%@,%d", doc.name,path.row);
[self deleteRow:doc];

}

- (void)deleteRow:(Player *)doc {

// ReferMeAppDelegate * appDelegate =(ReferMeAppDelegate *)[[UIApplication sharedApplication] delegate];     self.party = [PartyParser loadParty];     if(_party!= nil){         for(Player * player in _party.players){             NSLog(@“%@,%@”,player.name,player.alert);             NSComparisonResult resultName,resultEmail,resultPhone,resultLocation;             // NSString * str = [appDelegate.plistDict valueForKey:@“flag”];             // if([player.email length]!= 0&& player.alert == @“1”){                     resultName = [doc.name compare:player.name];                     resultEmail = [doc.email compare:player.email];                     resultPhone = [doc.phone compare:player.phone];                     resultLocation = [doc.location compare:player.location];                     if(resultName == 0&& resultEmail == 0&& resultPhone == 0&& resultLocation == 0){                         //lblName1.text = @“equal”;                         Player * playerr = [[Player alloc] init];                         playerr =玩家;                         [_party.players removeObject:playerr];                         // [_party.players removeObjectAtIndex:path.row];                         打破;                     }         //}         //if([player.email length] == 0&& player.alert == @“2”){                     其他{                         resultName = [doc.name compare:player.name];                         resultPhone = [doc.phone compare:player.phone];                         if(resultName == 0&& resultPhone == 0){                             Player * playerr = [[Player alloc] init];                             playerr =玩家;                             [_party.players removeObject:playerr];                             打破;
                        }                     }         }

        for (Player *player in _party.players) {
            NSLog(@"after match case player names");
            NSLog(@"%@", player.name);
        }
        [PartyParser saveParty:_party];
    }

}

我在app中使用GData xml,支持read&写操作。

1 个答案:

答案 0 :(得分:0)

deleteRowsAtIndexPaths:方法仅执行删除的视觉效果,您还必须手动删除数据源中的相应项目,以使其中的项目数与您的操作所期望的一致,例如:

//You need to remove item from data source
[yourMutableDataArray removeObjectAtIndex:indexPath.row]; 
// Tell table view to remove the corresponding item cell
[tableView deleteRowsAtIndexPaths:
             [NSArray arrayWithObject:indexPath] 
             withRowAnimation:UITableViewRowAnimationFade];//in this line app crash 
NSLog(@"delete row");
//[tableView reloadData]; // You do not need this line