4
我很難找到一個易於理解的教程,有關於從pList文件中獲取其數據的分段UITableView的教程。來自pList的分段UITableView
我遇到麻煩的事情是如何正確構造pList文件以適應2個不同的部分。
我很難找到一個易於理解的教程,有關於從pList文件中獲取其數據的分段UITableView的教程。來自pList的分段UITableView
我遇到麻煩的事情是如何正確構造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
我不斷收到錯誤,可能的NSArray不objectForKey迴應? – Andyy 2011-01-31 14:18:42