UITableView---初始化


#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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值