ViewController.m
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
//声明tableview
@property (nonatomic, strong) UITableView *tableView;
//数据源字典
@property (nonatomic, strong) NSMutableDictionary *dataSource;
//保存所有字体数组
@property (nonatomic, strong) NSArray *fontListArray;
//保存所有的 key 数组
@property (nonatomic, strong) NSMutableArray *allKeysArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载数据源
// [self loadDataSource];
/**< 实例化tableView */
//初始化
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//设置行高(默认为44)
_tableView.rowHeight = 70.0;
//1.设置数据源协议
_tableView.dataSource = self;
//委托
_tableView.delegate = self;
//配置快速索引样式
_tableView.sectionIndexColor = [UIColor blackColor];
//_tableView.sectionIndexBackgroundColor = [UIColor yellowColor];
/**< 延时加载 */
[self performSelector:@selector(loadDataSource) withObject:nil afterDelay:0.5];
[self.view addSubview:_tableView];
}
#pragma mark -- UITableViewDataSource协议实现
//一共有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//返回字典中key的个数
return _allKeysArray.count;
}
//指定分区有多少行 cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//1.取出当前分区对应的 key
NSString *key = _allKeysArray[section];
//2.取出当前key 所对应的字体名数组
NSArray *fontList = _dataSource[key];
return fontList.count;
}
//配置返回cell(配置表格行)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/* NSLog(@"分区 = %ld",indexPath.section);
NSLog(@"行数 = %ld",indexPath.row);
//初始化
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
//展示数据到cell上
cell.textLabel.text = _dataSource[indexPath.row];
cell.detailTextLabel.text = @"子标题";
return cell;*/
/**< tableView的重用机制(对上面代码的(内存)优化) */
// NSLog(@"分区 = %ld",indexPath.section);
// NSLog(@"行数 = %ld",indexPath.row);
//1.定义一个标识符
static NSString *cellIdentifer = @"cell";
//2.从可重用队列里取出cell(表格行)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
//3.如果cell不存在,则初始化
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
}
//展示数据到cell上
//1.取字体名数组
NSArray *fontList = _dataSource[_allKeysArray[indexPath.section]];
//2.赋值
cell.textLabel.text = fontList[indexPath.row];
return cell;
}
//分区的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20.0;
}
//分区表头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return _allKeysArray[section];
}
//配置快速索引
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return _allKeysArray;
}
//点击某行 cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"分区 = %ld",indexPath.section);
NSLog(@"行数 = %ld",indexPath.row);
//1.取字体名数组
NSArray *fontList = _dataSource[_allKeysArray[indexPath.section]];
//2.字体名
NSString *fontName = fontList[indexPath.row];
//推送
DetailViewController *detailVC = [[DetailViewController alloc]init];
detailVC.fontName = fontName;
[self.navigationController pushViewController:detailVC animated:YES];
}
//延时调用方法(实例化数据源)
- (void)loadDataSource{
/**< 实例化数据源 */
_dataSource = [NSMutableDictionary dictionary];
//获取系统的所有字体
_fontListArray = [UIFont familyNames];
//处理字体名数组
for (NSString *string in _fontListArray) {
//1.获取字体首字母,转换为大写
NSString *firstLetter = [[string substringToIndex:1] uppercaseString];
//2.取出当前首字母(Key),所对应的字体名数组(value)
NSMutableArray *value = [[NSMutableArray alloc]initWithArray:_dataSource[firstLetter]];
//3.把当前取出的字体添加到对应的字体名数组
[value addObject:string];
//4.排序
[value sortUsingSelector:@selector(caseInsensitiveCompare:)];
//5.更新数据源字典
[_dataSource setObject:value forKey:firstLetter];
}
//获取数据源字典所有 key
_allKeysArray = [_dataSource allKeys].mutableCopy;
//排序
[_allKeysArray sortUsingSelector:@selector(caseInsensitiveCompare:)];
//刷新表格,告诉数据源协议,再执行一遍协议方法
[_tableView reloadData];
}
@end
DetailViewController.m#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置背景颜色
self.view.backgroundColor = [UIColor whiteColor];
//初始化label
UILabel *label = [[UILabel alloc]initWithFrame:self.view.bounds];
//居中
label.textAlignment = NSTextAlignmentCenter;
//字体名和大小
label.font = [UIFont fontWithName:_fontName size:27];
//文本内容
label.text = _fontName;
//添加到视图
[self.view addSubview:label];
}
@end