简体   繁体   English

从pList(字典)到Tableview

[英]From pList (dictionary) to Tableview

I have an editable pList with a dictionary of strings in the format: 我有一个带有字符串字典的可编辑pList格式:

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. 字典的长度最多可以变成200个键。 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. 我想在带有UITableViewCellStyleValue2的tableview中显示键及其值。 At the moment I'm putting them in the tableview by writing all the keys to a global NSMutableAray at startup: 目前,我通过在启动时将所有键写入全局NSMutableAray的方式将它们放入表格视图中:

keysArray = [myDictionary allKeys];

and then in the tableview method, I'm putting them in by using normal indexPath as index to the array: 然后在tableview方法中,通过使用常规indexPath作为数组的索引将它们放入:

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). 这是因为'allKeys'函数以随机顺序返回键(它本身在字典中也没有租金顺序)。

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. 循环遍历全局数组并将该值用作另一个字典的键的整个方法非常复杂且笨拙,该方法是从extern pList文件加载的。 So I'm looking for either a way to sort an NSMutableArray by date, or a better way to implement what I want. 因此,我正在寻找一种按日期对NSMutableArray进行排序的方法,或者一种实现我想要的更好的方法。

Thank you for listening, John. 约翰,谢谢您的收听。

You can create a array sorted by date. 您可以创建按日期排序的数组。 You will indexed this array indexPath.row to ask the item value. 您将为此数组indexPath.row编制索引以询问项目值。

To sort an array you can use: 要对数组排序,可以使用:

-sortedArrayUsingComparator:

[array sortedArrayUsingComparator:^(id obj1, id obj2) { 
  // Your comparaison
}];

see this guide for block usage 有关块用法,请参阅本指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM