0

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

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

0 回答 0