简体   繁体   English

确定对象来自哪个词典

[英]Determine which dictionary an object came from

I have a PLIST file that stores issue information. 我有一个存储问题信息的PLIST文件。 At the top is an array, and within that array there are dictionaries. 顶部是一个数组,在该数组内有字典。 Within each dictionary is a string called "Date" with different dates. 每个词典中都有一个带有不同日期的字符串“ Date”。 I then display the date values in a table view. 然后,我在表格视图中显示日期值。

I am able to determine the text of the UITableview cell tapped (and therefore the date string), but how do I then access the other values within the same dictionary? 我能够确定被窃听的UITableview单元格的文本(以及日期字符串),但是然后如何访问同一词典中的其他值 If the I know that the date string is February 16, 2012, how do I get the "Download URL"? 如果我知道日期字符串为2012年2月16日,如何获取“下载URL”? Here is a picture of my PLIST: 这是我的PLIST的图片:

在此处输入图片说明

You should be getting the indexPath from the cell that is tapped. 您应该从被点击的单元格获取indexPath。 The indexPath.row should give you an index number that you can use to get the correct dictionary from your array of dictionaries. indexPath.row应该为您提供一个索引号,您可以使用该索引号从字典数组中获取正确的字典。 If you are not already keeping this array, consider doing so if it's a manageable size. 如果尚未保留此数组,请考虑这样做,如果它的大小可以控制。

NSDictionary *selectedIssue = [myArrayOfDictionaries objectAtIndex:indexPath.row];
NSSTring *downloadURL = [selectedIssues valueForKey:@"Download URL"];

Personally, I would never rely on the date being unique or formatted in any particular way. 就个人而言,我永远不会依赖于日期的唯一性或以任何特定方式设置日期。

You can sort your array (Issue List) by the issues date before you init your TableViewController with that data and than use the NSIndexPath of the cell that was tapped. 您可以在使用该数据初始化TableViewController之前,按照发布日期对数组(问题列表)进行排序,然后使用被窃听的单元格的NSIndexPath进行排序。

To sort the array with the dictionaries use this snippet: 要使用字典对数组进行排序,请使用以下代码段:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
[issues sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

After that you can use the indexPath like that: 之后,您可以像这样使用indexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *url = [[issues objectAtIndex:indexPath.row] valueForKey:@"download_url"];
}

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

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