jaytrixz
jaytrixz

Reputation: 4079

How to Load Plist to UITableView Objective-C

I have this plist setup:

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

How can I plot this inside my UITableView? My header titles will be the key "area" while the table cell labels the key "shop" and the subtitle labels the key "location". The coordinates key will be used to plot it on the mapview that loads when the user taps the table cell.

Thanks in advance!

Upvotes: 0

Views: 564

Answers (3)

Angelo
Angelo

Reputation: 533

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

Upvotes: 1

bs7
bs7

Reputation: 627

you can try this :

//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...
}

Then you create a new NSObject class and for each cell you init your object, for example :

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

Hope it helps :)

Upvotes: 0

Janosch H&#252;bner
Janosch H&#252;bner

Reputation: 1694

You have to create 3 NSDictionary. Dict 1: titles Dict 2: celltexts Dict 3: subtitles

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

Then put it into an array

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

in your titlesForHeaderInSection

headertitle = [titlearray objectAtIndex:indexPath.row];

Do the steps for cell text and subtitle again.

Upvotes: 0

Related Questions