将行移动到不同的部分

时间:2013-06-20 00:21:05

标签: uitableview nsmutablearray tableview

嘿伙计我在tableview上有两个部分,我有一个可更新的数组,只更新一个部分。我想将带有数组的部分的单元格移动到空白部分。我尝试为空白部分创建另一个数组,但我无法使其工作。这是我的moveRowAtIndexPath方法。

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{

id objectToMove = [myArray1 objectAtIndex:fromIndexPath.row];
id objectToMove2 = [myArray2 objectAtIndex:fromIndexPath.row];

//move objects between main section
[myArray1 removeObjectAtIndex:fromIndexPath.row];
[myArray1 insertObject:objectToMove atIndex:toIndexPath.row];


//move object from main section to blank section
 [myArray1 removeObjectAtIndex:fromIndexPath.row];
 [myArray2 insertObject:objectToMove atIndex:toIndexPath.row];

我的numberOfRowsInSection返回每个部分的相应数组计数,但仍然没有运气。当我进入编辑模式并尝试移动单元格时,它会消失。

1 个答案:

答案 0 :(得分:0)

使用此代码修复它。

NSString * cell = nil;
switch (fromIndexPath.section) {
    case 0:
        cell = [[myArray2 objectAtIndex:fromIndexPath.row] retain];
        [myArray2 removeObjectAtIndex:fromIndexPath.row];
        break;
    case 1:
        cell = [[myArray1 objectAtIndex:fromIndexPath.row] retain];
        [myArray1 removeObjectAtIndex:fromIndexPath.row];
        break;
    }

switch (destinationIndexPath.section) {
    case 0:
        [myArray2 insertObject:cell atIndex:destinationIndexPath.row];
        break;
    case 1:
        [myArray1 insertObject:cell atIndex:destinationIndexPath.row];
        break;

}

[cell release];