2011-01-31 3 views
4

pList 파일에서 데이터를 가져 오는 섹션 화 된 UITableView를 가지고있는 이해하기 쉬운 튜토리얼을 찾는 데 어려움을 겪고 있습니다.sectioned UITableView pList에서 출처

문제가 생기는 것은 pList 파일을 올바르게 구성하여 2 개의 다른 섹션을 제공하는 것입니다.

답변

7

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

내가 오류,있는 NSArray가 objectForKey에 응답하지 않을 수가 계속? – Andyy

+0

nvm 내가 그 오류를 해결했습니다. (jQuery과 *)있는 tableView numberOfRowsInSection : (NSInteger) 섹션 { \t INT (NSInteger)있는 tableView -하지만 지금은 내 테이블을 보여줍니다하지만 채워지지 않은, 아니 섹션이나 아무것도 =/ – Andyy

+0

필자는 ... 에 내 문제의 일부를 추적 i = [[[tableData objectAtIndex : section] objectForKey : @ "Rows"] count]; \t NSLog (@ "% i", i); \t return [[[self.tableDataSource objectAtIndex : section] objectForKey : @ "Rows"] count]; } 내 NSLog가 반환하는 것을 알려줍니다. – Andyy