来自.plist的分段UITableView

时间:2012-07-19 08:54:22

标签: ios uitableview

我有一个使用.plist的工作应用程序,它从服务器动态下载并填充UTableView并将许多数据项传递给详细视图。这很好,但是,我现在想要使用UITableView添加一个分段.plist,这会给我带来麻烦。

我有一个使用以下代码的工作版本。但我想重组.plist并试图让这个工作没有成功。我觉得解决方案很简单,但我没有得到它!我需要的是一个如何使用部分和可以提取信息的方法填充UITableView的示例。目前我可以按部分和数组中的字符串填充UITableView。但是如何在新的.pList结构中访问其他信息? (两者都显示在下面)非常感谢帮助。

现有。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>Title</key>
    <string>Banks</string>
    <key>Rows</key>
    <array>
        <string>Denizbank</string>
        <string>Garanti</string>
        <string>Denizbank ATM</string>
    </array>
</dict>
<dict>
    <key>Title</key>
    <string>Beach Clubs</string>
    <key>Rows</key>
    <array>
        <string>Kalkan Beach Club</string>
        <string>Villa Mahal</string>
        <string>Patara Prince</string>
    </array>
</dict>
   </array>
    </plist>

#import "DirectoryViewController.h"
#import "DetailListViewController.h"

@interface DirectoryViewController ()
//@property (nonatomic, retain) NSDictionary *dirItems;
@property (copy, nonatomic) NSArray* tableData;
@end

@implementation DirectoryViewController


@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;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSDictionary *rowData = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];  

    DetailListViewController *controller = [[DetailListViewController alloc] initWithNibName:@"DetailListViewController" bundle:nil];

    [self.navigationController pushViewController:controller animated:YES];

    //passes row info    
    controller.navigationItem.title = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];   
}
@end

正如我所说,这一切都运行正常,但我想使用重新设计的.plist结构,我想用它将数据传递给我的Detailview。

.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>Title</key>
    <string>Banks</string>
    <key>Rows</key>
    <array>
        <dict>
            <key>name</key>
            <string>Denizbank</string>
            <key>lat</key>
            <real>36.2654</real>
            <key>lon</key>
            <real>29.543</real>
            <key>info</key>
            <string>this is blah blan</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Garanti</string>
            <key>lat</key>
            <real>36.2654</real>
            <key>lon</key>
            <real>29.543</real>
            <key>info</key>
            <string>this is blah blan</string>
        </dict>
    </array>
</dict>
<dict>
    <key>Title</key>
    <string>Supermarkets</string>
    <key>Rows</key>
    <array>
        <dict>
            <key>name</key>
            <string>Migros</string>
            <key>lat</key>
            <real>36.2343</real>
            <key>lon</key>
            <real>29.543</real>
            <key>info</key>
            <string>this is blah blan</string>
        </dict>
        <dict>
            <key>name</key>
            <string>Yali</string>
            <key>lat</key>
            <real>36.2333</real>
            <key>lon</key>
            <real>29.5123</real>
            <key>info</key>
            <string>this is blah blan</string>
        </dict>
    </array>
</dict>
    </array>
    </plist>

非常感谢您的期待。

0 个答案:

没有答案