使用 plist 字典填充 UITableView [英] Populating a UITableView with plist dictonary

查看:27
本文介绍了使用 plist 字典填充 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在慢慢学习IOS编程的基础知识,但似乎遇到了一个小障碍

Been slowly picking up the basics in IOS Programming, but seemed to have hit a small hurdle

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

I have a table that gets populated from a plist located on a webserver

<?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>

现在,在进行了大量阅读之后,我设法将这段代码组合在一起以填充表格.

Now after doing a punt load of reading I have managed to hack together this code to populate a Table.

- (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

Now my table is populating but only with Item 0, Item 1, Item 2

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

My goal is to populate the table with eventTitle as the description.

很抱歉问了一个可能很愚蠢的问题,但我仍然试图理解目标 C

Sorry for the possibly dumb question but still trying to get my head around Objective C

推荐答案

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

You have to get the NSDictionary by your key in the function, and retrieve the corresponding data from your dictionary.

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
...
...

这篇关于使用 plist 字典填充 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆