0

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

- (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];
    }
}
4

3 回答 3

3

尝试改变

if (self.numberOfSections == 1)

到:

if (indexPath.section == 1)

希望能帮助到你。

于 2013-03-20T08:01:38.653 回答
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 文档

于 2013-03-20T08:17:35.500 回答
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];
    }
}
于 2013-03-20T08:18:49.273 回答