如何将Plist加载到UITableView Objective-C

时间:2012-10-07 08:58:59

标签: objective-c uitableview plist tableview

我有这个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>area</key>
                    <string>area1</string>
                    <key>shop</key>
                    <string>shop1</string>
                    <key>location</key>
                    <string>2nd Floor</string>
                    <key>coordinates</key>
                    <string>14.733836,121.058221</string>
            </dict>
            <dict>
                    <key>area</key>
                    <string>area1</string>
                    <key>shop</key>
                    <string>shop2</string>
                    <key>location</key>
                    <string>Ground Floor</string>
                    <key>coordinates</key>
                    <string>14.733836,121.058221</string>
            </dict>
            <dict>
                    <key>area</key>
                    <string>area2</string>
                    <key>shop</key>
                    <string>shop1</string>
                    <key>location</key>
                    <string>2nd Floor</string>
                    <key>coordinates</key>
                    <string>14.656918,121.029795</string>
            </dict>
            <dict>
                    <key>area</key>
                    <string>area2</string>
                    <key>shop</key>
                    <string>shop2</string>
                    <key>location</key>
                    <string>Ground Floor</string>
                    <key>coordinates</key>
                    <string>14.656918,121.029795</string>
            </dict>
        </array>
        </plist>

如何在UITableView中绘制这个?我的标题标题将是关键“区域”,而表格单元格标记关键字“商店”,字幕标记关键字“位置”。坐标键将用于在用户点击表格单元格时加载的mapview上绘制它。

提前致谢!

3 个答案:

答案 0 :(得分:1)

- (UITableViewCell *)tableView:cellForRowAtIndexPath:
NSDictionary *dict = [array objectAtIndex: indexPath.section];
cell.textLabel.text = [dict objectForKey: @"shop"];
cell.detailTextLabel.text = [dict objectForKey: @"location"];

- (NSString *)tableView:titleForHeaderInSection:
NSDictionary *dict = [array objectAtIndex: indexPath.section];
NSString *area = [dict objectForKey: @"area"];
return area;

- (void)tableView:didSelectRowAtIndexPath:
NSDictionary *dict = [array objectAtIndex: indexPath.section];
NSString *coordinates = [dict objectForKey: @"coordinates"];
newViewController.coordinates = coordinates

答案 1 :(得分:0)

您必须创建3个NSDictionary。 Dict 1:头衔 词典2:celltexts 字典3:字幕

NSDictionary * dictTitles = [[NSDictionary dictionaryWithContentsOfFile:@“filepathtoyourplist”] valueForKey:@“Titles”];

然后将其放入数组

NSArray * titleArray = [[NSArray alloc] initWithDictionary:dictTitles];

你的titlesForHeaderInSection

中的

headertitle = [titlearray objectAtIndex:indexPath.row];

再次执行单元格文本和副标题的步骤。

答案 2 :(得分:0)

你可以试试这个:

//In your .h file
NSArray *array;

//In your .m file
- (void)viewDidLoad {    
   // Some code...
   NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"plist"];
   array = [NSArray arrayWithContentsOfFile:path];
   // Some code...
}

然后创建一个新的NSObject类,并为每个单元格初始化对象,例如:

MyClass *newInstance = [[MyClass alloc] init];
newInstance.area = [array objectForKey:@"area"];
newInstance.shop = [array objectForKey:@"shop"];
newInstance.location = [array objectForKey:@"location"];
newInstance.coordinates = [array objectForKey:@"coordinates"];

希望有所帮助:)