用plist字典填充表视图

时间:2012-06-25 10:59:57

标签: ios xcode uitableview

慢慢接受了IOS编程的基础知识,但似乎遇到了一个小障碍

我有一个表,可以从位于网络服务器上的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填充表格作为描述。

对于可能愚蠢的问题感到抱歉,但仍然试图了解Objective C

2 个答案:

答案 0 :(得分:0)

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"]`

答案 1 :(得分:0)

您必须通过函数中的键获取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
...
...