如何使用按钮在 2 个部分之间移动单元格?

How to move a cell between 2 sections with a Button?

在我的 iOS 应用程序中,我试图将一个单元格从一个使用数组 (dataArray) 的部分移动到另一个使用另一个数组 (followedArray) 的部分,每个单元格中都有一个按钮。我设置了按钮,但找不到任何代码来通过按钮操作移动单元格。

我尝试添加一些代码以在 NSMutableArray 中添加和删除对象,但出现错误


*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'


代码进入-(void)followButtonClick:(id)sender

这是我尝试添加到我的按钮的代码,它通过一个标签进入每个单元格,因为我从服务器获取数组而不是手动输入。

     [myTableView beginUpdates];

     // Removing Cell from dataArray Section
    [dataArray removeObject: indexPath];
    NSInteger rowToRemove = indexPath.row;
    [myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];

    // Inserting Cell to followedArray Section
    [followedArray insertObject:indexPath atIndex:indexPath.row];
    NSInteger rowToAdd = indexPath.row;
    [myTableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES];

    [myTableView endUpdates];

我敢肯定这在很多层面上都是错误的,但我在这里画了一个空白。希望有人能给我指出正确的方向。

代码:

cellForRowAtIndexPath

// Configuring the cell
Data * dataObject;
if (!isFiltered) {

    if (indexPath.section == 0) {
        dataObject = [followedArray objectAtIndex:indexPath.row];
    }
    else {
        dataObject = [dataArray objectAtIndex:indexPath.row];
    }
}
else {
    dataObject = [filteredArray objectAtIndex:indexPath.row];
}

cell.followButton.tag = indexPath.row;
[cell.followButton addTarget:self action:@selector(followButtonClick:) forControlEvents:UIControlEventTouchUpInside];
cell.followButton.hidden = NO;

在每个单元格内点击跟随按钮

-(void)followButtonClick:(UIButton *)sender {

// Adding row to tag
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:buttonPosition];

// Creating an action per tag
if (indexPath != nil)
{
    NSLog(@"Current Row = %@", indexPath);

    // Showing Status Labels
    CustomCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
    cell.firstStatusLabel.hidden = NO;
    cell.secondStatusLabel.hidden = NO;

    // Change Follow to Following
    [sender setImage:[UIImage imageNamed:@"follow.png"] forState:UIControlStateNormal];
    cell.followButton.hidden = YES;
    cell.followedButton.hidden = NO;

    // ----- ERROR HERE -----
    [self.myTableView beginUpdates];

    // ----- Inserting Cell to Section 0 ----- *NOT WORKING*
    [followedArray insertObject:[gamesArray objectAtIndex:indexPath.row] atIndex:indexPath.row];
    NSInteger rowToAdd = indexPath.row;
    [self.myTableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToAdd inSection:0], nil] withRowAnimation:YES];

    // ----- Removing Cell from Section 1 ----- *WORKING*
    [gamesArray removeObjectAtIndex:indexPath.row];
    NSInteger rowToRemove = indexPath.row;
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];

    [self.myTableView endUpdates];
  }
}

filteredArray 用于我的搜索栏,项目是从 dataArray 过滤的。

dataArray 是我所有来自服务器的数据

followedArray 是从 dataArrayfilteredArray[= 单击按钮时我要放置单元格的位置54=].

如果您需要更多代码,请告诉我,希望我能解决这个问题,谢谢!

您可以从一个节中删除单元格,然后将单元格插入到另一个节中。

//update data Array1
[xxx removeObject:xxx];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowToRemove inSection:0]] withRowAnimation:YES];

//update data Array2
[xxx insertObject:xxx];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:rowToAdd inSection:0]] withRowAnimation: YES];

这就是帮助我修复错误的原因,感谢@AliOmari 和@Rachel

    [self.myTableView beginUpdates];

    [followedArray insertObject:[dataArray objectAtIndex:indexPath.row] atIndex:0];
    [myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

    [dataArray removeObjectAtIndex:indexPath.row];
    NSInteger rowToRemove = indexPath.row;
    [self.myTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:[NSIndexPath indexPathForRow:rowToRemove inSection:1], nil] withRowAnimation:YES];

    [self.myTableView endUpdates];