用plist填充UITableView

在IOS编程中慢慢地提到了基础知识,但似乎遇到了一个小障碍

我有一个表,从位于networking服务器上的plist填充

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Item 0</key> <dict> <key>eventDate</key> <string>27-06-2012</string> <key>eventTitle</key> <string>Clinic</string> </dict> <key>Item 1</key> <dict> <key>eventDate</key> <string>28-06-2012</string> <key>eventTitle</key> <string>Clinic</string> </dict> <key>Item 2</key> <dict> <key>eventDate</key> <string>28-06-2012</string> <key>eventTitle</key> <string>Office Closed</string> </dict> <key>Item 3</key> <dict> <key>eventDate</key> <string>29-06-2012</string> <key>eventTitle</key> <string>Tour</string> </dict> </dict> </plist> 

现在,在完成读取的加载之后,我设法将这些代码一起破解,以填充表格。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; { UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; } NSString *currentEventName; currentEventName = [eventKey_web objectAtIndex:indexPath.row]; [[cell textLabel] setText:currentEventName]; return cell; } ... - (void)viewDidLoad { [super viewDidLoad]; NSURL * myURL1; myURL1 = [NSURL URLWithString:@"http://www.mywebsite.com/events.plist"]; event_web = [[NSDictionary alloc] initWithContentsOfURL:myURL1]; eventKey_web = [event_web allKeys]; } 

现在我的表正在填充,但只有项目0,项目1,项目2

我的目标是用eventTitle作为描述来填充表格。

对不起,可能愚蠢的问题,但仍然试图让我的目标C的头

您必须通过您的键在函数中获取NSDictionary,并从您的字典中检索相应的数据。

 NSString *currentEventName = [eventKey_web objectAtIndex:indexPath.row]; // This is only the key from your root dict NSDictionary *currentEventDict = [event_web objectForKey:currentEventName]; NSString *currentEventDate = [currentEventDict objectForKey:@"eventDate"]; NSString *currentEventTitle = [currentEventDict objectForKey:@"eventTitle"]; // Here do what you need to show ... ... 

eventKey_web = [event_web allKeys]; 从你的plist获得所有的钥匙,你需要为该钥匙增加价值。

所以不要使用这个:

currentEventName = [eventKey_web objectAtIndex:indexPath.row];

你可以使用像这样的东西:

 NSMutableDictionary *r = [[event_web objectAtIndex:indexPath.row] objectForKey:[eventKey_web objectAtIndex:indexPath.row] ]; event_web = [r objectForKey:@"eventTitle"]`