Populate UITableView using NSMutableDictionary, keysSortedByValueUsingSelector
My model class has a NSMutableDictionary with my data. In my view class, I want to display that data in my UITableView cellforRowAtIndexPath method. My view class has an NSArray for the data to开发者_开发百科 display.
I can do this to get my data into my NSArray to display correctly:
self.CategoriesArray = [model.CategoryDictionary allKeys];
However, since NSMutableDictionary does not sort on its own, my items in my TableView are not in alphabetical order. I thought I could do this:
However, I get the following error:
2Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFBoolean localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0x13d0a20'
I'm assuming that's because my view class doesn't know what to do with the localizedCaseInsensitiveCompare method.
How do I solve this problem? Thanks.
I think you were saying that this gives you the correct results, just not in order, right?
self.CategoriesArray = [model.CategoryDictionary allKeys];
If that's the case, then this will give you the correct order if I'm understanding your question correctly:
NSArray* keys = [model.CategoryDictionary allKeys];
self.CategoriesArray = [keys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
localizedCaseInsensitiveCompare:
is only to compare NSString, not NSDictionary or NSArray.
To sort your categoriesArray look at
sortedArrayUsingComparator:
and sortedArrayUsingDescriptors:
for NSArray
sortUsingDescriptors
and sortUsingComparator
for NSMutableArray
To loop through your array you can do something like
for (NSString *string in categoriesArray)
{
}
精彩评论