Reputation: 101
I have a UITableView
that gets populated via an array (tableArray
), who gets populated from core data.
each UITableViewCell
gets assigned a number at creation time and the numbers are stored in an array. (numberArray
)
when the user reorders the rows, the numbers get moved around in the array (in conjunction with the tableView
of course)
So two Mutable arrays are used here.
The numberArray
holds the numbers (or the order) of the TableViewCells
.
I need to sort the array that holds the UITableViewCell
's text (tableArray
)
to reflect the same order that the numberArray
holds.
Also, this is important: as i said before, each cell gets assigned a number, this number is stored in the numberArray
,
I need both of the arrays to be sorted to hold the same values in the same place.
So for example:
tableArray
hold some objects:
1) hi
2) whats Up
3) this
4) is cool!
so as you can see each object here was assigned a number 1-4.
and each of these numbers is added to the numberArray
.
The user can move the cells around so obviously the order of the numbers will change.
So when the view loads up, i need to get the exact order of the numberArray
whether it is
1,2,3,4 or 2,4,3,1
and i need to sort the tableArray
to reflect the same order as the numberArray
so when the view loads up, if the numberArray's
order is 2,3,4,1 i want the tableArray's
order to be set to
2"whats up", 3"this", 4"is cool!", 1"hi".
I believe i can do this via NSPredicate
.
Any help is greatly appreciated!
EDIT
cellForRow:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * identifier = @"identifier";
self.myCell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (self.myCell == nil) {
self.myCell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
HandgunAmmo *handgunAmmo = [self.tableArray objectAtIndex:indexPath.row];
self.myCell.brandLabel.text = handgunAmmo.brand;
self.myCell.caliberLabel.text = handgunAmmo.caliber;
self.myCell.numberOfRoundsLabel.text = handgunAmmo.numberOfRounds;
return self.myCell;
}
And in my viewWIllAppear
method:
-(void)viewWillAppear:(BOOL)animated{
if (self.context == nil)
{
self.context = [(RootAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"HandgunAmmo" inManagedObjectContext:self.context];
[request setEntity:entity];
NSError *error;
NSMutableArray *array = [[self.context executeFetchRequest:request error:&error] mutableCopy];
[self setTableArray:array];
[self.ammoTable reloadData];
[super viewWillAppear:YES];
}
So, the reason why the array doesnt stay persistent when being changed is because im loading the data from core data, and i call [self setTableArray:array];
which reloads all of the data from core data into the array, then it populates the tableview with the array. So i need to be able to sort the array
before i set it equal to self.tableArray
.
Thank you for the help!
Upvotes: 0
Views: 1692
Reputation: 539965
Why don't you leave the tableArray
unchanged, and use the numberArray
as an index into the other array.
You would initialize the numberArray
to 0, 1, 2, ..., n-1
with
numberArray = [NSMutableArray array];
for (NSUInteger i = 0; i < [tableArray count]; i++) {
[numberArray addObject:[NSNumber numberWithUnsignedInteger:i]];
}
When you need an item, e.g. in cellForRowAtIndexPath
, you access it via the index:
NSUInteger i = [[numberArray objectAtIndex:row] unsignedIntegerValue];
NSString *item = [tableArray objectAtIndex:i];
Now you need to reorder the numberArray
only, and the changes will automatically be reflected in the table view.
Update: A good solution to handle the reordering of Core Data objects in a table view can be found here: UITableView Core Data reordering
Upvotes: 2
Reputation: 9836
NSMutableArray *unsortedArray = [NSMutableArray arrayWithObjects:@"1) hi",@"2) whats Up",@"3) this",@"4) is cool!",nil];
NSArray *guideArray = [NSArray arrayWithObjects:@"2",@"4",@"3",@"1", nil];
for(int i=0; i< [guideArray count];i++)
{
for(int j=0; j< [unsortedArray count];j++)
{
if([[[unsortedArray objectAtIndex:j] substringToIndex:[[guideArray objectAtIndex:i] length]] isEqualToString:[guideArray objectAtIndex:i]])
//if([[unsortedArray objectAtIndex:j] containsObject:[guideArray objectAtIndex:i]])
{
[unsortedArray exchangeObjectAtIndex:j withObjectAtIndex:i];
break;
}
}
}
NSLog(@"%@",unsortedArray);
OR
guideArray = @[@"2",@"4",@"3",@"1"];
unsortedArray = [@[@[@"1) hi"],
@[@"2) wats up "],
@[@"3) this,"],
@[@"4) cool"]] mutableCopy];
[unsortedArray sortUsingComparator:^(id o1, id o2) {
NSString *s1 = [o1 objectAtIndex:0];
s1 = [s1 substringToIndex:[s1 rangeOfString:@")"].location];
NSString *s2 = [o2 objectAtIndex:0];
s2 = [s2 substringToIndex:[s2 rangeOfString:@")"].location];
NSInteger idx1 = [guideArray indexOfObject:s1];
NSInteger idx2 = [guideArray indexOfObject:s2];
return idx1 - idx2;
}];
NSLog(@"%@",unsortedArray);
Try this hope this help partially.
Upvotes: 1
Reputation: 52227
I cant see a way to solve it with a predicate
NSArray *stringArray = @[@"hi", @"what's up?", @"this", @"is cool"];
NSArray *numberArray = @[@2, @4, @3, @1];
NSMutableArray *combinedArray = [NSMutableArray array];
//connect string with the numbers of there new position
[numberArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *string =stringArray[[obj integerValue]-1];
[combinedArray addObject:@[string, obj]];
}];
NSMutableArray *orderedArray = [NSMutableArray array];
[combinedArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[orderedArray addObject:obj[0]];
}];
NSLog(@"%@", orderedArray);
result
(
"what's up?",
"is cool",
this,
hi
)
Upvotes: 1