I'm implementing search bar in iphone app. I have list of Workers, each have attributes: firstName, lastName, company. Sections of table view are set to company attribute.
I did set predicate when searching:
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"firstName contains[cd] %@", searchBar.text];
and i get error:
NSFetchedResultsController ERROR: The fetched object at index 3 has an out of order section name 'company2. Objects must be sorted by section name'
when I have sortDescriptor:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
I did notice that when I change it to
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName"` ascending:YES se开发者_C百科lector:@selector(caseInsensitiveCompare:)];
now search works correctly with no error. Does initWithKey param must match attribute name in predicate? I don't get it.
From the NSFetchedResultsController docs:
The fetch request must have at least one sort descriptor. If the controller generates sections, the first sort descriptor in the array is used to group the objects into sections; its key must either be the same as sectionNameKeyPath or the relative ordering using its key must match that using sectionNameKeyPath.
In your case, you need to use two sorts (in an array.) Element one should be a sort on the company name attribute and second element should sort on the lastName/firstName attribute.
精彩评论