[英]Sectioned UITableview from 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> 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;
}
我想用部分顯示表。 節標題確實會出現,但是沒有內容。 我從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
鍵的數據。
像我一樣分界線有很多好處。 對於每個單元格,沒有理由不必多次查找分區數據,行數據和行數據。 因此,這效率更高。 拆分代碼還使調試變得更加容易,並且使閱讀也更加容易。
cell.textLabel.text = [[[_tableData objectAtIndex: indexPath.section] objectForKey: @"name"] objectAtIndex: indexPath.row];
cell.detailTextLabel.text = [[[_tableData objectAtIndex:indexPath.section] objectForKey:@"price"]objectAtIndex: indexPath.row];
應該是(可以自由使用文字)
cell.textLabel.text = _tableData[indexPath.section][@"Rows"][indexPath.row]@"name"];
cell.detailTextLabel.text = _tableData[indexPath.section][@"Rows"][indexPath.row][@"price"];
您錯過了引用稱為“行”的鍵的情況。 這是一個可能的解決方案:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
NSDictionary *product = [[[_tableData objectAtIndex:indexPath.section] objectForKey:@"Rows"] objectAtIndex:indexPath.row];
cell.textLabel.text = [product objectForKey:@"name"];
cell.detailTextLabel.text = [product objectForKey:@"price"];
return cell;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.