Reputation: 288
I have one problem with my table. I've made CoreData attribute1,2,3 where user can store NSStrings. This data i show in TableView with sortDescriptorWithKey: "attribute1". Then i have three buttons over my table, where user can manually change this description by taping on them. When he want arrange data by attribute1 or attribute2 everything work fine, but attribute3 doesn't work. Data in table are sorted by chaos.
Any help appreciated.
here is my code for these buttons:
-(IBAction)sortTable:(UIButton*)sender {
NSString *key = @"price";
if (sender.tag ==1) {
key = @"date";
} else if (sender.tag ==2) {
key = @"result";
}
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Data"];
request.sortDescriptors =
@[[NSSortDescriptor sortDescriptorWithKey:key ascending:NO]];
self.mutableArray = [NSMutableArray arrayWithArray:[self.managedObjectContext executeFetchRequest:request error:nil]];
[self.tableView reloadData];
}
Upvotes: 0
Views: 251
Reputation: 80271
Your code looks fine. Make sure you are actually displaying the attribute3
string in your table and not something else. Cross check with your data in the actual sqlite store.
Still, I think your code is quite redundant. Try to simplify. One way is to have your buttons have the tags 1, 2, and 3.
-(IBAction)sortTable:(UIButton*)sender {
NSString *key = [NSString stringWithFormat:@"attribute%d", sender.tag];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:"Data"];
request.sortDescriptors =
@[[NSSortDescriptor sortDescriptorWithKey:key ascending:NO]];
self.mutableArray = [NSMutableArray arrayWithArray:
[self.managedObjectContext executeFetchRequest:request error:nil];
[self.tableView reloadData];
}
Upvotes: 1