Question

I have a UITableView each cell shows unique menu item I want items to be sort in my list but those menu items are not coming from an array but from a db object

Example:

cellForRowAtIndexPath{
  Menus_Items *menuItem = [menuCategory.hasMenuItems.allObjects objectAtIndex:indexPath.row];
  lblMenuItemName.text = menuItem.name;

}

How can I sort menu items ? I could have done it if it was an array i.e.

NSSortDescriptor *orderSort = [[NSSortDescriptor alloc] initWithKey:@"order" ascending:NO];
// String are alphabetized in ascending order
NSSortDescriptor *strNameSort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

// Combine the two
NSArray *sortDescriptors = @[orderSort, strNameSort];

arrCategories = [DatabaseHelper getAllData:@"Menus_cat" offset:0 predicate:[NSPredicate predicateWithFormat:@"ofOutlet == %@",outletsObject] sortDescriptor:nil];

// sortedArrayUsingDescriptor is the method use to sort an array which is having multiple parameters in sort descriptor
arrCategories = [arrCategories sortedArrayUsingDescriptors:sortDescriptors];

Help please. Thanks in advance

Était-ce utile?

La solution

menuCategory.hasMenuItems.allObjects is an array so you can sort it to get the appropriate item. You could sort it each time or keep a sorted cache of the content. Neither of these options is ideal.

Really, you want to use a fetch request with a sort descriptor, ideally managed by a fetch request controller. Usually you would do that by using the relationship 'backwards'. So, you create the fetch and set the predicate to XXX = %@, where XXX is the relationship name and the supplied parameter is menuCategory.

This is assuming the relationship is 1:many, if it's many:many you will need a different predicate form.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top