gpt4 book ai didi

ios - 来自 plist 的分段 UITableview

转载 作者:行者123 更新时间:2023-11-28 19:10:54 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com