从表中删除单元格 - 提供代码

时间:2012-02-14 15:32:36

标签: iphone objective-c cocoa-touch uitableview

我有tableviewController。在UINavigationBar我将添加一个名为编辑的按钮。当用户点击它时,我需要所有单元格进入编辑模式,在那里他们可以删除记录。

我想以下方法可以做到。我对么 ?当我点击编辑按钮时,我会在单元格上看到红色圆圈和删除按钮吗?

2.。)当用户点击它以调用以下方法时,如何编写“编辑”按钮(UIBarbuttonitem)的代码?

3.)当我们删除单元格时,我需要重新加载表格中的数据。我为此编写了代码,是否正确。 (目前我不在我的Mac上)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [self.tableView beginUpdates];
        [discardedItems addObject:[self.entries objectAtIndex:indexPath.row]];
        [self.itemsMutableArray removeObjectsInArray:discardedItems ]; 
        self.entries = [NSArray arrayWithArray:self.itemsMutableArray];
        [self.tableView endUpdates];

        [self.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
    }   
}

2 个答案:

答案 0 :(得分:1)

首先,您需要指定是否可以编辑行。这可以通过以下方法完成

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

如果您希望所有行都可以编辑,则返回yes。

要获得红色圆圈,请使用

也许在编辑按钮调用的方法中......

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit ", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(pressedEdit:)];
self.navigationItem.leftBarButtonItem = editButton;

(在viewdidload中)

并在pressedEdit中:

- (void) pressedEdit :(id)sender {
    UIBarButtonItem *editButton = (UIBarButtonItem*)self.navigationItem.leftBarButtonItem;

    if (!self.tabeView.editing) {
        [self.tableView setEditing:YES animated:YES];
        editButton.title = NSLocalizedString(@"Done", @"");
    }
    else {
        [self.tableView setEditing:NO animated:YES];
        editButton.title = NSLocalizedString(@"Edit", @"");
    }
}

根据您编写的代码,我认为您应首先更新数据源,然后删除单元格...

答案 1 :(得分:1)

您的代码似乎没有错。但这么少的工作线路如此之多。您也可以使用reloadData终止动画。并且你包装的调用甚至不会触及beginUpdates / endUpdates中的tableView。

为什么不首先使用NSMutableArray作为数据源?您可以将七行代码减少为两行:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // delete the item from the datasource
        [self.entries removeObjectAtIndex:indexPath.row];
        // Delete the row from the table view
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    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
    }   
}
相关问题