[英]Sectioned UITableView from pList that can drill down into children sub views
[英]sectioned UITableView sourced from a pList
我很難找到一個易於理解的教程,該教程有一個分段的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.