UITableView---初始化

来源:互联网 发布:易安卓播放器源码 编辑:程序博客网 时间:2024/04/30 04:33


#import "ViewController.h"


@interface ViewController ()

{

    NSArray * _array;

}


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    //加载数据

    [self_loadData];

    

    //初始化

    UITableView * tableView=[[UITableViewalloc]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.viewaddSubview:tableView];

}


- (void) _loadData

{

    _array=[UIFontfamilyNames];

    

}



//    <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=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"mytable"];

    

   NSLog(@"Hello%ld",indexPath.row); //调用次数

    cell.textLabel.text=_array[indexPath.row];   //cell中存放的内容

    cell.textLabel.font=[UIFontfontWithName:_array[indexPath.row]size:16];    //设置内容的字体大小

    

    

   return cell;

}




@end


0 0
原创粉丝点击