#import "ViewController.h"
@interface ViewController ()
{
NSArray * _array;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载数据
[self _loadData];
//初始化
UITableView * tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20) style:UITableViewStylePlain];
//tableView有两个代理
tableView.dataSource=self; //1 专门加载数据
tableView.delegate =self; //2 监听---实现某种方法
tableView.rowHeight=100; //设置每一行的高度
//tableView.estimatedRowHeight=79;
//加载到视图
[self.view addSubview:tableView];
}
- (void) _loadData
{
_array=[UIFont familyNames];
}
// <UITableViewDataSource>协议 必须 实现的 两个方法
#pragma mark - 1.返回当前行数
//返回当前tableView的行数 ---用来控制行数多少
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _array.count;
}
#pragma mark - 2.将数据放到单元格
//根据行数,new出新的单元格 放到当前行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//返回值是 UITableViewCell * 类型,所以初始化一个 UITableViewCell * 类型的对象
UITableViewCell * cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mytable"];
NSLog(@"Hello%ld",indexPath.row); //调用次数
cell.textLabel.text=_array[indexPath.row]; //cell中存放的内容
cell.textLabel.font=[UIFont fontWithName:_array[indexPath.row] size:16]; //设置内容的字体大小
return cell;
}
@end