部分UITableView来源于pList

我很难find一个易于理解的教程,有一个分区的UITableView从pList文件中获取其数据。

事情我有麻烦,是如何正确地构造pList文件,以迎合2个不同的部分。

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