Reputation: 27
i have a UITableView that a user can add and rearrange objects in... when the user moves an item, i try to re-arrange the data array to fit what the user has changed his cell order to... i am obviously not doing it correctly... here is my code
- (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;
}
Upvotes: 0
Views: 91