0

我的“财产清单2”如下:

<?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>Police</key>
    <dict>
        <key>UserName</key>
        <string>PoliceStation</string>
        <key>Image</key>
        <string>PoliceStation.png</string>
        <key>Number</key>
        <string>1241241244</string>
        <key>Address</key>
        <string>TaichungNorth</string>
    </dict>
        <key>Fire</key>
    <dict>
        <key>UserName</key>
        <string>FireFighter</string>
        <key>Image</key>
        <string>FireFighter.png</string>
        <key>Number</key>
        <string>21324124</string>
        <key>Address</key>
        <string>TaichungEast</string>
    </dict>
        <key>Taichung</key>
    <dict>
        <key>UserName</key>
        <string>TaichungGovernment</string>
        <key>Image</key>
        <string>TaichungGovernment.jpg</string>
        <key>Number</key>
        <string>3252342342</string>
        <key>Address</key>
        <string>TaichungWest</string>
    </dict>
    </dict>
</plist>

如何将每个“用户名”和“图像”加载到 UITableView 中?尤其是当类型是“字典”而不是“数组”时?

UITableview 将在中心显示每个“用户名”文本,在左侧显示每个“图像”。

4

1 回答 1

0

您可以将 plist 解析为字典:

NSString *path = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"plist"];  
NSDictionary *objects = [NSDictionary dictionaryWithContentsOfFile:path];

比您可以迭代所有键并创建对象以在表格视图中呈现:

NSMutableArray *parsedObjects = [NSMutableArray array];
[objects enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    Object *object = [[Object alloc] init];
    [object setUserName:obj[@"UserName"]];
    ...
    [parsedObjects addObject:object];
}];

比你应该实现 tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
     ...
     Object *object = parsedObjects[indexPath.row];
     [cell setObject:object];

     return cell;
  }

并实现数据在单元格中显示。

于 2013-10-13T07:41:41.020 回答