ios - 来自 plist 的 UITableView 加载到字典的 NSArray 中

标签 ios uitableview nsarray nsdictionary plist

我已经阅读了很多有关此主题的操作方法,但我一直无法弄明白。

如果我有这样的 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">
<array>
    <dict>
        <key>color</key>
        <string>brown</string>
        <key>height</key>
        <real>40</real>
        <key>length</key>
        <real>75</real>
        <key>level</key>
        <real>2</real>
        <key>name</key>
        <string>bear</string>
        <key>note</key>
        <string>ugly, but strong</string>
        <key>width</key>
        <real>30</real>
    </dict>
    <dict>
        <key>color</key>
        <string>tan</string>
        <key>height</key>
        <real>20</real>
        <key>length</key>
        <real>30</real>
        <key>level</key>
        <real>4</real>
        <key>name</key>
        <string>dog</string>
        <key>note</key>
        <string>cute</string>
        <key>width</key>
        <real>8</real>
    </dict>
    <dict>
        <key>color</key>
        <string>chestnut</string>
        <key>height</key>
        <real>75</real>
        <key>length</key>
        <real>90</real>
        <key>level</key>
        <real>3</real>
        <key>name</key>
        <string>horse</string>
        <key>note</key>
        <string>fast</string>
        <key>width</key>
        <real>22</real>
    </dict>
    <dict>
        <key>color</key>
        <string>gray</string>
        <key>height</key>
        <real>20</real>
        <key>length</key>
        <real>85</real>
        <key>level</key>
        <real>2</real>
        <key>name</key>
        <string>shark</string>
        <key>note</key>
        <string>hungry!</string>
        <key>width</key>
        <real>35</real>
    </dict>
</array>
</plist>

我可以像这样加载数据并打印整个列表:

NSString *path = [[NSBundle mainBundle]pathForResource:@"testdata" ofType:@"plist"];
NSMutableArray *foodsList = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(@"Did Load - FoodsList = %@", foodsList);

但是当我将它加载到 UITableView 中以便我可以得到一个名称列表时,我无法让它工作。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = [[testdata objectAtIndex:indexPath.row]objectForKey:@"name"];

    return cell;
}

NSMutableArray *keyValues = testdata[key];字典和数组类型之间的行失败。

我做错了什么?

谢谢,

最佳答案

您没有在 cellForRowAtIndexPath 内的任何位置设置文本:

设置单元格标签文本

cell.textLabel.text = @"Hello World";

关于ios - 来自 plist 的 UITableView 加载到字典的 NSArray 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21592737/

相关文章:

ios - 将存储在 NSDictionary 中的 JSON 数据中的应用程序名称存储在 NSArray 中

objective-c - NSArray的实例变量

iphone - 如何实现连续拖拽菜单效果?

ios - CollectionView 单元格中的间距

ios - 如何从 xib 加载特定 View 属性?

ios - UITableview 的触摸事件

ios - 如何在1个ViewController中实现2个UITableView?每个 tableView 都有自己的自定义单元格

ios - 防止顶部 UiTableViewCell 滚动

ios - Tesseract 3.01 和 Pocket-OCR 构建问题

ios - 如何在 swift 3 中将 NSArray 转换为 Swift 数组