简体   繁体   中英

sectioned UITableView sourced from a pList

I'm having trouble finding a easy to understand tutorial on having a sectioned UITableView which gets its data from a pList file.

Things im having trouble with, is how to properly structure the pList file to cater for 2 different sections.

The root of the plist should be an array. The array should contain two dictionaries (your sections). The dictionaries will contain two keys: one for section title and one for the rows in the section.

Assuming you read your plist into an NSArray* sections, you can return the section, row count, section title and cell titles using the code below.

Your plist file would look like this:

<?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>Title</key>
        <string>Section1</string>
        <key>Rows</key>
        <array>
            <string>Section1 Item1</string>
            <string>Section1 Item2</string>
        </array>
    </dict>
    <dict>
        <key>Title</key>
        <string>Section2</string>
        <key>Rows</key>
        <array>
            <string>Section2 Item1</string>
            <string>Section2 Item2</string>
        </array>
    </dict>
</array>
</plist>




#import "RootViewController.h"

@interface RootViewController ()

@property (copy, nonatomic) NSArray* tableData;

@end


@implementation RootViewController

@synthesize tableData;

- (void) dealloc
{
    self.tableData = nil;
    [super dealloc];
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    self.tableData = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"Table" ofType: @"plist"]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return [tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return [[[tableData objectAtIndex: section] objectForKey: @"Rows"] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
{
    return [[tableData objectAtIndex: section] objectForKey: @"Title"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];

    return cell;
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM