【发布时间】:2013-03-10 13:40:29
【问题描述】:
我有一个在线列表(格式为http://example.com/people.plist)。如何让 UITable 视图从 plist 而不是静态数组中提取名称?
<plist version="1.0">
<array>
<dict>
<key>fname</key>
<string>Scott</string>
<key>sname</key>
<string>Sherwood</string>
<key>age</key>
<string>30</string>
</dict>
<dict>
<key>fname</key>
<string>Janet</string>
<key>sname</key>
<string>Smith</string>
<key>age</key>
<string>26</string>
</dict>
<dict>
<key>fname</key>
<string>John</string>
<key>sname</key>
<string>Blogs</string>
<key>age</key>
<string>20</string>
</dict>
</array>
</plist>
这是我的 viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
Person *p1 = [[Person alloc] initWithFname:@"Scott" sname:@"Sherwood" age:30];
Person *p2 = [[Person alloc] initWithFname:@"Janet" sname:@"Smith" age:26];
Person *p3 = [[Person alloc] initWithFname:@"John" sname:@"Blogs" age:20];
self.people = [NSArray arrayWithObjects:p1,p2,p3, nil];
}
这是我的 tableView cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
Person *p1 = [self.people objectAtIndex:indexPath.row];
cell.textLabel.text = p1.fname;
return cell;
}
【问题讨论】:
-
您想获取存储在远程服务器中的 plist 还是要将 plist 与应用程序捆绑在一起并显示其内容? BTW plist 的 url 不可访问
-
谢谢。首先,正确的 URL 是格式,而不是我的 plist 的实际 URL。我添加了我的 plist,所以你可以看到。其次,我希望它是在线的,而不是打包的。 :)
-
SO中有一个类似的帖子。我想这对你来说会很好stackoverflow.com/a/10973246/767730
-
嘿。感谢您的回复。不幸的是,这是关于将 plist 写入文件,我不需要这样做。本质上,我只是想从 plist 中提取该数组。谢谢! :)
-
请注意,这与 Xcode完全无关。
标签: ios objective-c arrays plist