【问题标题】:moving rows into different sections将行移动到不同的部分
【发布时间】:2013-06-21 01:58:49
【问题描述】:

大家好,我的 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 返回每个部分的适当数组计数,但仍然没有运气。当我进入编辑模式并尝试移动单元格时,它消失了。

【问题讨论】:

    标签: uitableview nsmutablearray tableview


    【解决方案1】:

    用这段代码修复它。

    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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 2019-08-12
      • 2022-11-22
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多