使用 NSMutableDictionary、keySortedByValueUsingSelector 填充 UITableView
我的模型类有一个包含我的数据的 NSMutableDictionary。在我的视图类中,我想在 UITableView cellforRowAtIndexPath 方法中显示该数据。我的视图类有一个 NSArray 用于显示数据。
我可以这样做来将数据放入 NSArray 中以正确显示:
self.CategoriesArray = [model.CategoryDictionary allKeys];
但是,由于 NSMutableDictionary 不会自行排序,因此 TableView 中的项目不按字母顺序排列。我以为我可以这样做:
但是,我收到以下错误:
2Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFBoolean localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0x13d0a20'
我假设那是因为我的视图类不知道如何处理 localizedCaseInsensitiveCompare 方法。
我该如何解决这个问题?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你是说这给了你正确的结果,只是不按顺序,对吗?
如果是这样的话,如果我正确理解你的问题,这将为你提供正确的顺序:
I think you were saying that this gives you the correct results, just not in order, right?
If that's the case, then this will give you the correct order if I'm understanding your question correctly:
localizedCaseInsensitiveCompare:
仅比较 NSString,而不是 NSDictionary 或 NSArray。要对categoriesArray进行排序,请查看 NSArray 的
sortedArrayUsingComparator:
和sortedArrayUsingDescriptors:
sortUsingDescriptors
和 NSMutableArray 的sortUsingComparator
循环遍历您的数组你可以做类似的事情
localizedCaseInsensitiveCompare:
is only to compare NSString, not NSDictionary or NSArray.To sort your categoriesArray look at
sortedArrayUsingComparator:
andsortedArrayUsingDescriptors:
for NSArraysortUsingDescriptors
andsortUsingComparator
for NSMutableArrayTo loop through your array you can do something like