ios - 来自 plist 的分段 UITableview

标签 ios objective-c uitableview plist

我有一个 plist 文件,内容如下(源代码 View ):

<?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> Food</string>
        <key>Rows</key>
        <array>
            <dict>
                <key>name</key>
                <string>Chunky Chips</string>
                <key>price</key>
                <string>25,-</string>
            </dict>
            <dict>
                <key>name</key>
                <string>Chunky Chips w/ Cheese</string>
                <key>price</key>
                <string>29,-</string>
            </dict>
            <dict>
                <key>name</key>
                <string>Fish'n'Chips</string>
                <key>price</key>
                <string>89,-</string>
            </dict>
        </array>
    </dict>
    <dict>
        <key>Title</key>
        <string>Snacks</string>
        <key>Rows</key>
        <array>
            <dict>
                <key>name</key>
                <string>Crisps</string>
                <key>price</key>
                <string>15,-</string>
            </dict>
            <dict>
                <key>name</key>
                <string>Hot Nuts</string>
                <key>price</key>
                <string>20,-</string>
            </dict>
            <dict>
                <key>name</key>
                <string>Popcorn</string>
                <key>price</key>
                <string>25,-</string>
            </dict>
        </array>
    </dict>
    <dict>
        <key>Title</key>
        <string>Draught Beer</string>
        <key>Rows</key>
        <array>
            <dict>
                <key>name</key>
                <string>The Ale</string>
                <key>price</key>
                <string>47,-</string>
            </dict>
            <dict>
                <key>name</key>
                <string>Carlsberg</string>
                <key>price</key>
                <string>47,-</string>
            </dict>
            <dict>
                <key>name</key>
                <string>Kilkenny</string>
                <key>price</key>
                <string>47,-</string>
            </dict>
        </array>
    </dict>
</array>
</plist>

我的 TableViewController:

#import "MenuViewController.h"

@interface MenuViewController ()

@property (copy, nonatomic) NSArray* tableData;

@end

@implementation MenuViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableData = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"TestList" ofType: @"plist"]];
    NSLog(@"%@",_tableData);

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:(BOOL)animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    //return [[[_tableData objectAtIndex: section] objectForKey: @"Rows"] count];
    NSDictionary *dataInSection = [[_tableData objectAtIndex: section] objectForKey: @"Rows"];
    return [dataInSection count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    return [[_tableData objectAtIndex: section] objectForKey: @"Title"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MenuCell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [[[_tableData objectAtIndex: indexPath.section] objectForKey: @"name"] objectAtIndex: indexPath.row];
    cell.detailTextLabel.text = [[[_tableData objectAtIndex:indexPath.section] objectForKey:@"price"]objectAtIndex: indexPath.row];

    return cell;
}

我想显示带有部分的表格。 Sectionheaders 确实出现了,但是没有内容。我从 nslog 中得到以下信息:

(
        {
        Rows =         (
                        {
                name = "Chunky Chips";
                price = "25,-";
            },
                        {
                name = "Chunky Chips w/ Cheese";
                price = "29,-";
            },
                        {
                name = "Fish'n'Chips";
                price = "89,-";
            }
        );
        Title = " Food";
    },
        {
        Rows =         (
                        {
                name = Crisps;
                price = "15,-";
            },
                        {
                name = "Hot Nuts";
                price = "20,-";
            },
                        {
                name = Popcorn;
                price = "25,-";
            }
        );
        Title = Snacks;
    },
        {
        Rows =         (
                        {
                name = "The Ale";
                price = "47,-";
            },
                        {
                name = Carlsberg;
                price = "47,-";
            },
                        {
                name = Kilkenny;
                price = "47,-";
            }
        );
        Title = "Draught Beer";
    }
)

知道为什么表格单元格中没有填充相应的名称和价格吗?

最佳答案

这是一个很好的例子,说明了为什么不应该在一行中进行如此长的嵌套方法调用。你的线路:

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

应该是:

NSDictionary *sectionData = _tableData[indexPath.section];
NSArray *rowsData = sectionData[@"Rows"];
NSDictionary *rowData = rowsData[indexPath.row];
NSString *name = rowData[@"name"];
NSString *price = rowData[@"price"];

cell.textLabel.text = name;
cell.detailTextLabel.text = price;

按照您的方式,您忘记了获取 Rows 键的数据。

像我一样拆分行有很多好处。对于每个单元格,没有理由必须多次查找部分数据、行数据和行数据。所以这样效率更高。拆分代码还使调试更容易,也更易于阅读。

关于ios - 来自 plist 的分段 UITableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15639857/

相关文章:

ios - 在后台线程上将核心数据图像加载到 UITableViewCell 时崩溃

jquery - 在 iOS 上的文本字段上设置编程焦点正在工作但不显示 CURSOR

iphone - 如何确定 iPhone/iPad 中的应用程序下载日期

ios - 如何使用具有不同应用程序名称和 info.plist 的相同来源创建两个项目?

iphone - 如何从第二个 View Controller 以编程方式创建选项卡栏

ios - 在 iOS 应用程序中管理网络调用的库/框架

ios - PushViewController 两次当我双击太快时

ios - 使用 AVPlayer 将实时流的音轨静音

ios - 在 Objective C 中用字符串替换正则表达式?

ios - 移动行(在 :to:) doesn't work if the UITableView has many items