如何滑动以删除特定部分中的行

时间:2013-03-20 07:55:51

标签: ios objective-c uitableview sections

我有两个数组。每个数组都有一个部分。当用户滑动行并选择删除时,它必须删除右侧部分中的相应行。如何在正确的部分更改我的代码以使其删除。  到目前为止,这是我的代码。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //remove the deleted object from your data source.
        //If you're data source is an NSMutableArray, do this
        if (self.numberOfSections == 1)
        {
            [self.playerNames removeObjectAtIndex:indexPath.row];
        }
        else
        {
            //code here needs to determine which section needs to be deleted
        }
        [self.tableView reloadData];
    }
}

3 个答案:

答案 0 :(得分:3)

尝试更改

if (self.numberOfSections == 1)

为:

if (indexPath.section == 1)

希望它有所帮助。

答案 1 :(得分:1)

以下是一些代码段:

-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
    forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) {
          NSInteger row = [indexPath row];
          [tableDataSource removeObjectAtIndex:row];      
          [tableView reloadData];
    }
}

另请参阅此Apple Documentation

答案 2 :(得分:0)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //remove the deleted object from your data source.
        //If you're data source is an NSMutableArray, do this
        if (self.numberOfSections == 1)
        {

            [self.playerNames removeObjectAtIndex:indexPath.row];
        }
        else
        {
             if (indexPath.section == 0)
             {
                 [self.team1 removeObjectAtIndex:indexPath.row];
             }
            else
            {
                [self.team2 removeObjectAtIndex:indexPath.row];
            }
        }
        [self.tableView reloadData];
    }
}