Reputation: 1056
I have an Array of 3 objects for example [0] , [1] , [2] and after swapping my TableRows from the delegate
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
NSLog(@"fromIndexPath.row %d",fromIndexPath.row);
NSLog(@"toIndexPath.row %d",toIndexPath.row);
NSLog(@"%@",masterArray);
for (id object in masterArray) {
for(int indexValue = 0; indexValue < [self.finalContactsArr count]; indexValue++){
if([[self.finalContactsArr objectAtIndex:indexValue] isEqual:object])
NSLog(@"the indexValue %d", [self.finalContactsArr indexOfObject:object]);
}
}
item = @"";
///// This is working fine as below. I want to update the above loop for getting index everytime and log whole array as UI is working absolutely perfect from below code
item = [self.finalContactsArr objectAtIndex:fromIndexPath.row];
[self.finalContactsArr removeObject:item];
[self.finalContactsArr insertObject:item atIndex:toIndexPath.row];
}
I am looking for new swap order for example , if I swap 0, 1, 2 to to 1, 0 , 2 or 2, 0 , 1, so should get same index Orders "the indexValue" = 1, 0, 2. If I managed it in a seperate array so I will be getting original indexes which is stored in masterArray but the thing is for example I am swapping 2, 0 , 1 to 1, 2 ,0 "last is inserted on 1st index and others are pushed down as removeObject and insertObject logic mentioned below on UI is working" and now after completion it will start from beginning 0, 1, 2. It should be like stack push and then after completion should be from beginning 0, 1 , 2. No need to retain anything.
Upvotes: 0
Views: 227
Reputation: 1056
Please find the answer below and I also thank @Deepak Bharti for his answer too.
// 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
{
NSLog(@"fromIndexPath.row %d",fromIndexPath.row);
NSLog(@"toIndexPath.row %d",toIndexPath.row);
if ([matchReplaceOriginalArr count] > 0)
[matchReplaceOriginalArr removeAllObjects];
self.matchReplaceOriginalArr = [matchReplaceArr mutableCopy]; ///// Managing this array to hold original index value in each case
NSLog(@"matchReplaceOriginalArr %@",matchReplaceOriginalArr);
item1 = @"";
item1 = [self.matchReplaceArr objectAtIndex:fromIndexPath.row];
[self.matchReplaceArr removeObjectAtIndex:fromIndexPath.row];
[self.matchReplaceArr insertObject:item1 atIndex:toIndexPath.row];
for (id objectFrom_myarray in matchReplaceArr) {
for(id objectFrom_staticMyArray in matchReplaceOriginalArr)
{
if([objectFrom_myarray isEqual: objectFrom_staticMyArray])
{
NSLog(@"the indexValue %d \t and Object is %@",[matchReplaceOriginalArr indexOfObject: objectFrom_staticMyArray],objectFrom_myarray);
}
}
}
NSLog(@"matchReplaceArr %@", matchReplaceArr);
}
Upvotes: 0
Reputation: 280
Here is one solution to your requirements.
Step 1: Make a property as follow:
@property (strong, nonatomic) NSMutableArray * staticMyArray;
Step 2:
Add following to - (void)viewDidLoad
method
self.staticMyArray = [[NSMutableArray alloc]init];
self.staticMyArray = [myarray copy];
Step 3:
Finally add the following at end in - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
method
NSLog(@"New Order of Indexes is:");
for (id objectFrom_myarray in myarray)
{
for(id objectFrom_staticMyArray in self.staticMyArray)
{
if([objectFrom_myarray isEqual: objectFrom_staticMyArray])
{
NSLog(@"%d",[self.staticMyArray indexOfObject: objectFrom_staticMyArray]);//break;
}
}
}
Let me know, if it worked or not.
Upvotes: 1
Reputation: 1542
Modify Array as per Cell-identifier. Which you have created before swapped and later.
Upvotes: 0
Reputation: 122421
Call the method [UITableView moveRowAtIndexPath:toIndexPath:]
on the tableview object, or if there are many rows swapped, simply use [UITableView reloadData]
.
Upvotes: 1