用UITableView创建字体列表
ViewController.m
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
//声明tableView
@property (nonatomic,strong) UITableView *tableView;
//声明数据源字典
@property (nonatomic,strong) NSMutableDictionary *dataSourceArray;
//保存所有字体数组
@property (nonatomic, strong)NSArray *fontListArray;
//保存所有的 key 数组
@property (nonatomic, strong)NSMutableArray *allkeyArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//实例化tableView
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.rowHeight = 60;//行高
//1.数据源协议
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
//延时加载
[self performSelector:@selector(loadDataSource) withObject:nil afterDelay:0];
//配置快速索引样式 sectionIndexColor 字母的颜色
_tableView.sectionIndexColor = [UIColor redColor];
//右边栏的背景颜色
_tableView.sectionIndexBackgroundColor = [UIColor yellowColor];
}
#pragma mark - loadDataSource
- (void)loadDataSource{
//实例化数据源
_dataSourceArray = [NSMutableDictionary dictionary];
//获取系统的所有字体
_fontListArray = [UIFont familyNames];
//打印字体
// NSLog(@"%@",_fontListArray);
//处理字体名数组
for (NSString *string in _fontListArray) {
//1.获取字体首字母,转换成大写
NSString *firstLetter = [[string substringToIndex:1] uppercaseString];
//2.取出当前首字母(key),所对应的字体名数组(value)
NSMutableArray *value = [[NSMutableArray alloc] initWithArray:_dataSourceArray[firstLetter]];
//3.把当前取出的字体添加到对应的字体名数组
[value addObject:string];
//4.排序
[value sortUsingSelector:@selector(caseInsensitiveCompare:)];
//5.更新数据源字典
[_dataSourceArray setObject:value forKey:firstLetter];
}
NSLog(@"%@",_dataSourceArray);
//获取字典的所有 key
_allkeyArray = [_dataSourceArray allKeys].mutableCopy;
//排序
[_allkeyArray sortUsingSelector:@selector(caseInsensitiveCompare:)];
//等到数据源有值过后,必须再次刷新数据源协议
[_tableView reloadData];
}
#pragma mark - <UITableViewDataSource>
//一共有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _allkeyArray.count;
}
//每一列多少行(指定分区有多少行 cell)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//取出当前分区对应的 key
NSString *key = _allkeyArray[section];
//取出当前 key 所对应的字体名数组
NSArray *fontList = _dataSourceArray[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.textLabel.text = _dataSourceArray[indexPath.row];
cell.detailTextLabel.text = @"子标题";
return cell;
*/
//tableView 的重用机制
//1.定义一个标识符
static NSString *cellIndentifer = @"cell";
//2.从重用队列里取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer];
//3.如果cell不存在,则初始化
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifer];
}
//字体数组
NSArray *fontList = _dataSourceArray[_allkeyArray[indexPath.section]];
//赋值
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 _allkeyArray[section];
}
//配置快速索引
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return _allkeyArray;
}
//点击某行 cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"分区 = %ld",indexPath.section);
NSLog(@"行数 = %ld",indexPath.row);
//字体数组
NSArray *fontList = _dataSourceArray[_allkeyArray[indexPath.section]];
//字体名
NSString *fontName = fontList[indexPath.row];
DetailViewController *detailVc = [[DetailViewController alloc] init];
detailVc.fontName = fontName;
[self.navigationController pushViewController:detailVc animated:YES];
}
@end
DetailViewController.m
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
label.font = [UIFont fontWithName:_fontName size:27];
label.textAlignment = NSTextAlignmentCenter;
label.text = _fontName;
[self.view addSubview:label];
}
@end