UITableView单元重组阵列麻烦

时间:2013-07-28 15:47:09

标签: ios objective-c arrays uitableview nsmutableorderedset

我有一个UITableView,用户可以添加和重新排列对象...当用户移动项目时,我尝试重新排列数据数组以适应用户将其单元格顺序更改为... i我显然没有正确地做到这一点......这是我的代码

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    NSMutableOrderedSet *temp = [[NSMutableOrderedSet alloc]init];
    if(fromIndexPath.row == toIndexPath.row){
        temp = _dataArray;
    }
    int min,max;
    if(fromIndexPath.row > toIndexPath.row){min = toIndexPath.row;max = fromIndexPath.row;}
    if(fromIndexPath.row < toIndexPath.row){max = toIndexPath.row;min = fromIndexPath.row;}
    for(int i = 0; i < _dataArray.count; i++)
    {
        NSLog(@"%i",i);
        if(i == toIndexPath.row)
        {
            [temp insertObject:_dataArray[fromIndexPath.row] atIndex:i];
        }
        else
        {
            if(i >= min && i <= max)
            {
                if(fromIndexPath.row > toIndexPath.row)
                {
                    [temp insertObject:_dataArray[i] atIndex:i+1];
                }
                else if(fromIndexPath.row < toIndexPath.row)
                {
                    [temp insertObject:_dataArray[i] atIndex:i-1];
                }
            }
            else
            {
                [temp insertObject:_dataArray[i] atIndex:i];
            }
        }
    }
    NSLog(@"================ Cell Array Testing ================");
    for(int i = 0; i < temp.count; i++)
    {
        NSLog(@"Cell: %@",temp[i]);
    }
    _dataArray = temp;
}

0 个答案:

没有答案