#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSArray *_dataList;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//分組情況下 創(chuàng)建表視圖
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
//設(shè)置代理對象
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
/**
* 從工程目錄中讀取文件
*/
//1.獲取文件路徑 bundle 獲取工程目錄的方法
//bundle 在括號里面打不出來 另換一行打 然后放在括號里面
NSString *path = [[NSBundle mainBundle]pathForResource:@"font" ofType:@"plist"];
//2.通過路徑加載容器對象
_dataList = [NSArray arrayWithContentsOfFile:path];
NSLog(@"%@",_dataList);
}
#pragma mark --UITableViewDataSource
//可選方法:返回 組個數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//組個數(shù)
return _dataList.count;
}
//返回 每個組有多少個單元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//1.從datalist中獲取section下標(biāo)對應(yīng)的對象--->小數(shù)組(盛放的NSString*)
NSArray *subArray = _dataList[section];
return subArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//1.
static NSString *identifier = @"font_cell";
//2.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//3.
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//1.不分組情況
// cell.textLabel.text = [_dataList objectAtIndex:indexPath.row];
//
// cell.textLabel.font = [UIFont fontWithName:[_dataList objectAtIndex:indexPath.row] size:20];
//2.分組
//(1)在datalist中 找到section對應(yīng)的二級數(shù)組
NSArray *subArray = _dataList[indexPath.section];
//(2)在二級數(shù)組中 找到row對應(yīng)的字符串對象
cell.textLabel.text = subArray[indexPath.row];
cell.textLabel.font = [UIFont fontWithName:subArray[indexPath.row] size:20];
return cell;
}
@end
屏幕快照 2016-03-03 上午9.55.19.png