UI之Tableviewcell


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

- (void)viewDidLoad {

    [super viewDidLoad];

    [self createTableView];

}

- (void)createTableView{

    //UITableView继承自UIScrollView  是一个可以上下滚动,左右不能滚动的的视图,称为表试图,contentsize自动计算

    //UITableViewStylePlain系统样式

    UITableView*tableV = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

    /*

     UITableViewStylePlain,                 

     UITableViewStyleGrouped                

     */

    //UITableViewDelegate(主要是设置UITableViewUI样式)

    tableV.delegate= self;

    //UITableViewDataSource(主要设置数据源的显示)

    tableV.dataSource= self;

   

    [self.view addSubview:tableV];

}

#pragmamark -- UITableViewDelegate

//设置tableView的行(row)的高度

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

    //行的高度默认是44

    return 50;

}

//设置tableView的区(section)的个数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    //区的个数默认是1

    return 6;

}

#pragmamark -- UItableViewDataSource

//每个分区有多少行(每个section有多少个row

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    //根据下载的数据确定有多少行

    return 10;

}

//UITableViewCell单元格tableView组成(显示)的最重要的部分

//每一行的单元格是什么样子的

//tableView每次滚动都会调用这个方法

//“重用机制UItableViewCell在创建的时候只会创建固定个数,然后当下面的Cell显示时候,会利用已经消失的cell进行显示数据(消失的cell会存放在一个"重用池"里面),党要使用已经消失的cell的时候,会重用池里面去拿,而不是重新创建,这样大大的提高了内存的使用效率

//如果cell样式不同创建新的cell

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //定义一个重用标识符(名字自己定义)

    static NSString *cellID = @"cellID";

    //先从重用池(重用队列)里面去拿cell,根据重用标识符

    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    //如果拿不到,创建cell

    if (cell== nil) {

        //创建系统样式的cell,在创建的时候设置标识

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

    }

    //indexPath代表rowsection

    //indexPath.row当前分区的行

    //indexPath.sectiontableView的区

    cell.textLabel.text = [NSStringstringWithFormat:@"%d分区第%d",indexPath.section,indexPath.row];

    cell.imageView.image = [UIImageimageNamed:@"火影01"];

    returncell;

}

设置标尾(区尾)

//-(NSString *)tableView:(UITableView *)tableViewtitleForFooterInSection:(NSInteger)section{

//   

//    return [NSString stringWithFormat:@"标尾%c",'A'+section];

//}

设置标头(区头)

//-(NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{

//    return [NSString stringWithFormat:@"标头%c",'A'+section];

//}

//标头的高度

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{

   

    return 60;

}

//标尾的高度

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{

    return 60;

}

//自定制标头标尾的样式

- (UIView *)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section{

    UIView*view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 60)];

    //xywidth无效,

    view.backgroundColor= [UIColor colorWithRed:arc4random()%256/255.0f green:arc4random()%256/255.0f blue:arc4random()%256/255.0f alpha:1];

    returnview;

}

- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{

    UIView*view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];

    view.backgroundColor= [UIColor colorWithRed:arc4random()%256/255.0f green:arc4random()%256/255.0f blue:arc4random()%256/255.0f alpha:1];

    returnview;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值