@interface SectionsViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSDictionary *names;
NSArray *keys;
}
@property (nonatomic, retain) NSDictionary *names;
@property (nonatomic, retain) NSArray *keys;
@end
Теперь переключитесь на SectionsViewController.m и добавьте следующий код в начало этого файла:
#import "SectionsViewController.h"
@implementation SectionsViewController
@synthesize names;
@synthesize keys;
- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:@"YOURpLISTfILE" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.names = dict; [dict release];
NSArray *array = [[names allKeys] sortedArrayUsingSelector: @selector(compare:)];
self.keys = array;
}
И добавьте следующий код в конец файла, чуть выше объявления @end:
#pragma mark
#pragma mark Table View Data Source Methods -
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [keys count];
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{ NSString *key = [keys objectAtIndex:section]; NSArray *nameSection = [names objectForKey:key];
return [nameSection count];
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section]; NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SectionsTableIdentifier;
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row]; return cell;
(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *key = [keys objectAtIndex:section]; return key;
}
@end