如何将 Plist 加载到 UITableView Objective-C



我有这个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 中绘制它?我的标题标题将是关键"区域",而表格单元格将键标记为"商店",副标题标记键"位置"。坐标键将用于在用户点击表格单元格时加载的地图视图上绘制它。

提前感谢!

- (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

你必须创建3个NSDictionary。字典1:标题字典2:单元格文本字典3:字幕

NSDictionary *dictTitle = [[NSDictionary dictionaryWithContentOfFile:@"filepathtoyourplist"] valueForKey:@"Titles"];

然后将其放入数组中

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

在您的标题中对于标题在部分

headertitle = [titlearray objectAtIndex:indexPath.row];

再次执行单元格文本和字幕的步骤。

你可以试试这个:

//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"];

希望对:)有所帮助

最新更新