2011-01-31 9 views
4

pListファイルからデータを取得するセクション化されたUITableViewを持つことについてわかりやすいチュートリアルを見つけるのが難しいです。sectioned UITableView pListからのソース

問題は、pListファイルを適切に構造化して、2つの異なるセクションに対応する方法です。

答えて

7

plistのルートは配列である必要があります。配列には2つのディクショナリ(セクション)が含まれている必要があります。辞書には2つのキーが含まれます:1つはセクションタイトル用で、もう1つはセクション内の行用です。

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 
+0

、NSArrayのはobjectForKeyに応答しないことがありますか? – Andyy

+0

nvm私はそのエラーを修正しました。しかし、今、私の表に示しますが、その未実装、無セクションか何か=/ – Andyy

+0

IVEは... に私の問題の一部をトレースし - (NSInteger)のtableView:(のUITableView *)のtableView numberOfRowsInSection:(NSInteger)セクション{ \t int型i = [[[tableData objectAtIndex:section] objectForKey:@ "Rows"]カウント]; \t NSLog(@ "%i"、i); \t戻り値[[[self.tableDataSource objectAtIndex:section] objectForKey:@ "Rows"] count]; } 私のNSLogは返ってきます0 – Andyy