1、创建一个表视图的两种样式
UITableViewStylePlain -----> 平铺的效果
UITableViewStyleGrouped -----> 分组的效果
2、表视图的结构
a) 表视图由头部是视图,尾部视图,中间由一连串的单元格组成
b) 表视图的头部由tableHeaderView属性设置,尾部由tableFooterView属性设置c) 分组表格由一连串的section视图组成
.m文件中
#import "ViewController.h"
//1. 实现UITableViewDataSource协议,如果不实现,就无法加载数据
@interface ViewController () <UITableViewDataSource>
@property(nonatomic, strong) NSArray *fNames;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, 200, 500) style:UITableViewStylePlain];
// 设置数据源代理:如果不设置,数据就无法显示
tableView.dataSource = self;
// 添加到视图中
[self.view addSubview:tableView];
// 初始化字体数组
_fNames = [UIFont familyNames];
}
#pragma mark - 返回单元格的分组个数
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
#pragma mark - 必须实现的数据协议
//2. 返回分组的单元格个数
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
//返回的单元格的个数 ----> 字体的个数
return _fNames.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//1) 创建一个单元格
/**
UITableViewCellStyleDefault //默认
UITableViewCellStyleValue1 //字体改变
UITableViewCellStyleValue2 //蓝色字体
UITableViewCellStyleSubtitle //自适应
*/
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//2) 设置单元格的内容:indexPath.section ------> 代表分组
// indexPath.row --------> 代表行
cell.textLabel.text = [NSString stringWithFormat:@"组:%ld, 行:%ld", indexPath.section, indexPath.row];
//3) 设置字体内容
cell.textLabel.text = _fontNames[indexPath.row];
//4) 设置字体样式
cell.textLabel.font = [UIFont fontWithName:_fontNames[indexPath.row] size:20];
return cell;
}