如何从UITableView中删除一行?

时间:2013-12-27 02:25:36

标签: ios objective-c uitableview

我想通过使用alertView消息确认从tableView中删除一行,但输出了一些错误:

reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

在SomeViewController界面中设置一些私有成员

@interface SomeViewController ()
{
    NSMutableArray *listItems;
    NSString *itemId;
    NSIndexPath *rowIndexPath;
}

在SomeViewController.m中

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [listItems count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[listItems objectAtIndex:section] count];
}

#pragma mark - Table view operation

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //add code here for when you hit delete
        UIAlertView *deleteAlert = [[UIAlertView alloc] initWithTitle:@"Message"
                                                                 message:@"Are you sure?"
                                                                delegate:self
                                                       cancelButtonTitle:@"OK"
                                                       otherButtonTitles:@"Cancel", nil];

        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        itemId = cell.textLabel.text;
        rowIndexPath = indexPath;
        deleteAlert.tag = 1;
        [deleteAlert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1)
    {
        if (buttonIndex == 0) {

            NSLog(@"itemId: %@", itemId);

            NSLog(@"rowIndexPath: %@", rowIndexPath);

            [self.tableView beginUpdates];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:rowIndexPath]
                                  withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView endUpdates];

            [self.tableView reloadData];
        }
    }
}

我可以正确设置tableView数据并显示删除选项,但无法弄清楚该错误发生的原因。

3 个答案:

答案 0 :(得分:1)

删除单元格之前删除数据源。

希望有所帮助

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1)
    {
        if (buttonIndex == 0) {

            NSLog(@"itemId: %@", itemId);

            NSLog(@"rowIndexPath: %@", rowIndexPath);

            [self.tableView beginUpdates];
            [[listItems objectAtIndex:section] removeObjectAtIndex:rowIndexPath.row]
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:rowIndexPath]
             withRowAnimation:UITableViewRowAnimationFade];
             [self.tableView endUpdates];
            [self.tableView reloadData];
    }
}

答案 1 :(得分:1)

请记住,每次执行删除操作时,都应该首先修改数据源。

答案 2 :(得分:0)

您只需从object中移除array,然后重新加载UITableView。像所以:

[YourArray removeObjectAtIndex:1]; //Change the index to which cell you want to "delete"
[YourTableView reloadData];

或按此处所述:How to delete a row from UITableView