从 pList(字典)到 Tableview
我有一个可编辑的 pList,其中包含以下格式的字符串字典:
21-10-2010 -> @"item1"
20-10-2010 -> @"item2"
19-10-2010 -> @"item3"
18-10-2010 -> @"item4"
17-10-2010 -> @"item5"
ect...
因此,以字符串格式的日期作为键,以具有不同信息的字符串作为值。字典的长度最多可达200个键。而且钥匙不一定按顺序来,并且可能会丢失一些日期。
我想使用 UITableViewCellStyleValue2 在表格视图中显示键及其值。目前,我通过在启动时将所有键写入全局 NSMutableAray 将它们放入 tableview 中:
keysArray = [myDictionary allKeys];
然后在 tableview 方法中,我使用普通的 indexPath 作为数组的索引将它们放入:
cell.textLabel.text = [keysArray objectAtIndex:indexPath];
cell.detailTextLabel.text = [myDictionary objectForKey: [keysArray objectAtIndex:indexPath]];
这是有效的,但日期不按顺序排列,最高日期在前。这是因为“allKeys”函数以随机顺序返回键(字典本身也没有顺序)。
循环遍历全局数组并将该值作为另一个字典的键(从 extern pList 文件加载)的整个方法确实非常复杂和尴尬。所以我正在寻找一种按日期对 NSMutableArray 进行排序的方法,或者一种更好的方法来实现我想要的。
谢谢你的聆听,约翰。
I have an editable pList with a dictionary of strings in the format:
21-10-2010 -> @"item1"
20-10-2010 -> @"item2"
19-10-2010 -> @"item3"
18-10-2010 -> @"item4"
17-10-2010 -> @"item5"
ect...
So with dates in stringformat as keys, and strings with different information as value. The lenght of the dictionary can become up to 200 keys. And the keys don't necessarily come in order, and there might be missing some dates.
I want to show the keys and their values in a tableview with UITableViewCellStyleValue2. At the moment I'm putting them in the tableview by writing all the keys to a global NSMutableAray at startup:
keysArray = [myDictionary allKeys];
and then in the tableview method, I'm putting them in by using normal indexPath as index to the array:
cell.textLabel.text = [keysArray objectAtIndex:indexPath];
cell.detailTextLabel.text = [myDictionary objectForKey: [keysArray objectAtIndex:indexPath]];
This is working, but the dates are not in order with the highest date first. This is because the 'allKeys'-function returns the keys in random order (also there arent order in the dictionary it self).
The whole method of cycling through a global array, and reffering this value as a key to another dictionary, that's loaded from a extern pList file, is really complicated and awkward. So I'm looking for either a way to sort an NSMutableArray by date, or a better way to implement what I want.
Thank you for listening, John.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个按日期排序的数组。您将对此数组indexPath.row建立索引以询问项目值。
要对数组进行排序,您可以使用:
请参阅此块使用指南
You can create a array sorted by date. You will indexed this array indexPath.row to ask the item value.
To sort an array you can use:
see this guide for block usage