Adding sections to a plist for Xcode for use in Cocoa touch Table View

Posted by Steve on Stack Overflow See other posts from Stack Overflow or by Steve
Published on 2010-01-11T19:18:22Z Indexed on 2010/04/04 23:03 UTC
Read the original article Hit count: 482

Filed under:
|
|

I am a beginning iPhone SDK programmer. I built a simple practice application I am trying to use to learn more about table views. It's an app that loads football teams from a plist and displays them in a table view with their stadium name and logo. Tapping the team goes to a detail view for that team.

I am trying to understand how to add sections to this, so that I might have a couple of teams in one section and others in another section, etc.

I would assume I need to both re-structure my plist and change the code to read arrays from the different levels of the plist?

To begin, I had a plist with the root array consisting of 3 dictionaries, one for each team. Each dictionary had 3 keys, "name" "stadium" and "logo". This works fine. I am loading it via:

NSString *path = [[NSBundle mainBundle] pathForResource:@"teams" ofType:@"plist"];
teams = [[NSMutableArray alloc] initWithContentsOfFile:path];

and then

// Configure the cell.
NSDictionary *team = [teams objectAtIndex:indexPath.row];
cell.textLabel.text = [team objectForKey:@"name"];
NSString *imgPath = [team valueForKey:@"logo"]; 
cell.imageView.image = [UIImage imageNamed:imgPath];
cell.detailTextLabel.text =[team objectForKey:@"stadium"];
return cell;

No problem. But now I wanted the sections, so I changed my plist to:

<array>
<dict>
    <key>teams 1</key>
    <array>
        <dict>
            <key>name</key>
            <string>Packers</string>
            <key>stadium</key>
            <string>Lambeau Field</string>
            <key>logo</key>
            <string>packers.jpg</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Jets</string>
            <key>stadium</key>
            <string>Giants Stadium</string>
            <key>logo</key>
            <string>jets_logo.jpg</string>
        </dict>
    </array>
</dict>
<dict>
    <key>teams 2</key>
    <array>
        <dict>
            <key>name</key>
            <string>Cincinnati Bengals</string>
            <key>stadium</key>
            <string>Paul Brown Stadium</string>
            <key>logo</key>
            <string>bengals.jpg</string>
        </dict>
    </array>
</dict>

And I am unsure how to modify the viewDidLoad to assign the sections to one NSArray and the teams "level" to another array.

© Stack Overflow or respective owner

Related posts about xcode

Related posts about iphone-sdk