分段UITableView源自pList [英] sectioned UITableView sourced from a pList

查看:165
本文介绍了分段UITableView源自pList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一个易于理解的教程,有一个区段的UITableView从pList文件获取其数据。

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

有麻烦的事情,如何正确地构造pList文件以适应2个不同的部分。

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

推荐答案

plist的根应该是一个数组。该数组应包含两个字典(您的部分)。字典将包含两个键:一个用于节标题,一个用于节中的行。

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.

假设您将plist读入NSArray *节,

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.

您的plist文件将如下所示:

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

这篇关于分段UITableView源自pList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆