上一篇我们使用了viewDidLoad初始化数组的方式,现在就是用 data。plist的方式进行初始化。
重点:1.plist的文件格式。
2.文件的查找方式
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tableView.dataSource =self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
self.dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path ];
self.array = [[self.dictionary allKeys]sortedArrayUsingSelector:@selector(compare:)];
// self.dictionary = [NSBundle mainBundle]
// Do any additional setup after loading the view from its nib.
}
3.关于cell的绘制的不同以及numberOfrows的改变
#pragma mark ---UItableViewDataSource Methods---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.dictionary objectForKey:[self.array objectAtIndex:section]] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIndetifiter = @"CellIndetifiter";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifiter];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifiter];
}
NSInteger section = [indexPath section];
NSArray *tempArray = [self.dictionary objectForKey:[self.array objectAtIndex:section]];
cell.textLabel.text = [tempArray objectAtIndex:[indexPath row]];
cell.accessoryType = UITableViewCellAccessoryDetailButton;
return cell;
}
4.关于titleHeader的设置
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.array objectAtIndex:section];
}
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.array;
}