Reputation: 47069
I created simple project with ARC, in which I have UITableView
. And I have NSMutableArray
of NSMutableDictionary
.
like
(
{
comment = "";
"field_name" = "field_name 1";
"main_id" = 1;
"order_index" = 0;
"picker_value" = "";
},
{
comment = "";
"field_name" = "field_name 2";
"main_id" = 2;
"order_index" = 1;
"picker_value" = "";
},
{
comment = "";
"field_name" = "field_name 3";
"main_id" = 3;
"order_index" = 2;
"picker_value" = "";
},
{
comment = "";
"field_name" = "field_name 4";
"main_id" = 4;
"order_index" = 3;
"picker_value" = "";
},
)
Value of key "field_name"
is displayed on each Cell of my UITableView
. It is working properly. But I want to put swap functionality on my UITableViewCell
.
For Example:
1 value - 0 order_index
2 value - 1 order_index -----
3 value - 2 order_index |-> I Want to swap value 2 to 4…(But do not want to change order_index)
4 value - 3 order_index -----
It should Be
1 value - 0 order_index
4 value - 1 order_index
3 value - 2 order_index
2 value - 3 order_index
value of dictionary will swap but do not want to change order_index
I know about methods of UITableView
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row > self.MyArray.count-1)
return NO;
return YES;
}
// Process the row move. This means updating the data model to correct the item indices.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
// here i get fromIndexPath.row and toIndexPath.row
NSMutableDictionary *oldDict = [NSMutableDictionary dictionary];
oldDict = [(NSMutableDictionary *)[self.listOfReportField objectAtIndex:fromIndexPath.row] copy];
[self.myArray removeObject:oldDict];
[self.myArray insertObject:[self.myArray objectAtIndex:toIndexPath.row-1] atIndex:fromIndexPath.row];
[self.myArray insertObject:oldDict atIndex:toIndexPath.row];
// Here i am not swap my row , what should be i need to do here ??
}
b
As my Requirement first I want to swap value
of dictionary but not order_index
here. I am not able to swap value.
Please give me suggestion.
Thanks in advance.
Upvotes: 1
Views: 1539
Reputation: 46543
Step 1: Save the order index value as well it actual index (1st in your case) i.e. "order_index" = 1;
and 4th one also.
Step 2: Swap(2nd value with 4th value).
Step 3: As it is array, replace the index value to the actual index(1st) and same for 4th.
EDIT: the full working code:
-(void)swapArray:(NSMutableArray *)array MaintaingOrderFrom:(NSInteger)swapFrom swapTo:(NSInteger)swapTo{
[array exchangeObjectAtIndex:swapFrom withObjectAtIndex:swapTo];
NSString *tempOrderIndex=array[swapFrom][@"order_index"];
array[swapFrom][@"order_index"]=array[swapTo][@"order_index"];
array[swapTo][@"order_index"]=tempOrderIndex;
}
Note: The array and dictionary inside should be Mutable.
EDIT 2: To check, I need to create your data model
NSMutableDictionary *dict1=[NSMutableDictionary dictionaryWithDictionary:@{@"comment": @"",
@"field_name":@"field_name 1",
@"main_id":@"1",
@"order_index":@"0",
@"picker_value":@""
}];
NSMutableDictionary *dict2=[NSMutableDictionary dictionaryWithDictionary:@{@"comment": @"",
@"field_name":@"field_name 2",
@"main_id":@"2",
@"order_index":@"1",
@"picker_value":@""
}];
NSMutableDictionary *dict3=[NSMutableDictionary dictionaryWithDictionary:@{@"comment": @"",
@"field_name":@"field_name 3",
@"main_id":@"3",
@"order_index":@"2",
@"picker_value":@""
}];
NSMutableDictionary *dict4=[NSMutableDictionary dictionaryWithDictionary:@{@"comment": @"",
@"field_name":@"field_name 4",
@"main_id":@"4",
@"order_index":@"3",
@"picker_value":@""
}];
NSMutableArray *array=[NSMutableArray arrayWithArray:@[dict1,dict2,dict3,dict4]];
[self swapArray:array MaintaingOrderFrom:1 swapTo:3];
NSLog(@"%@",array);
Upvotes: 3
Reputation: 7171
NSMutableArray has a method - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2
Why not call that method using your two values fromIndexPath and toIndexPath?
If I'm reading your question correctly, you want the "order_index" K/V pair in the dictionary to match that of the order of the array? After you've swapped the values using the method above, you could just call:
[[self.myArray objectAtIndex:fromIndexPath.row] setObject:[NSNumber numberWithInt:fromIndexPath.row] forKey:@"order_index"];
[[self.myArray objectAtIndex:toIndexPath.row] setObject:[NSNumber numberWithInt:toIndexPath.row] forKey:@"order_index"];
Upvotes: 1