2011-01-31 3 views
4

У меня возникли проблемы с поиском легко понятного учебника по поводу секционированного UITableView, который получает свои данные из файла pList.секция UITableView, полученная из pList

Вещи, у которых возникли проблемы с, заключается в том, как правильно структурировать файл pList для обслуживания двух разных разделов.

ответ

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 i исправил эту ошибку. Но теперь моя таблица показывает, но его не заселены, никаких разделов или что-нибудь =/ – Andyy

+0

ив проследили часть моей проблемы с ... - (NSInteger) Tableview: (UITableView *) Tableview numberOfRowsInSection: раздел (NSInteger) { \t ИНТ i = [[[tableData objectAtIndex: section] objectForKey: @ "Rows"] count]; \t NSLog (@ "% i", i); \t return [[[self.tableDataSource objectAtIndex: section] objectForKey: @ "Rows"] count]; } мой NSLog сообщает мне о его возвращении 0 – Andyy

Смежные вопросы