【问题标题】:sectioned UITableView sourced from a pList来自 pList 的分段 UITableView
【发布时间】:2011-01-31 12:08:22
【问题描述】:

我很难找到一个易于理解的教程,该教程介绍了从 pList 文件获取数据的分段 UITableView。

我遇到的问题是如何正确构建 pList 文件以适应 2 个不同的部分。

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios


    【解决方案1】:

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

    假设您将 plist 读入 NSArray* 部分,您可以使用以下代码返回部分、行数、部分标题和单元格标题。

    您的 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>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
    

    【讨论】:

    • 我不断收到错误,NSArray 可能无法响应 objectForKey ?
    • nvm 我修复了这个错误。 BUt 现在我的表格显示,但它的未填充,没有部分或任何东西 =/
    • 我将我的问题的一部分追踪到... - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { int i = [[[tableData objectAtIndex: section] objectForKey: @ “行”] 计数]; NSLog (@"%i", i); return [[[self.tableDataSource objectAtIndex: section] objectForKey:@"Rows"] count]; } 我的 NSLog 告诉我它返回 0
    • 在我的答案中添加了工作代码 - 您确定 tableData 在您检查时不是 nil 吗?在 viewWillAppear 中添加一个 reloadData 以获得良好的衡量标准!
    • 非常感谢您。问题在于我最初加载的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2011-12-14
    相关资源
    最近更新 更多