2013-10-12 44 views
0

我的「屬性列表2」有如下:的plist類型:百科到UITableView的

<?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中會顯示在中心每個「用戶名」文本和左側的每個「圖像」

回答

0

您可以分析的plist到dictonary:

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; 
    } 

並實現數據在顯示單元格。

+0

你好,AIKo。 我有問題的代碼: 'Object * object = [[Object alloc] init]; [object setUserName:obj [@「UserName」]];' Object是什麼?它是否適用於UITableView? 和對象適用於?並沒有一個名爲setUserName的方法.. 對不起,我剛學了1個月的iOS編程.. 請幫忙,謝謝! – user2871337