UITableView 入门浅谈

春节过了,没时间更新了,估计要好一段时间不能写了。今天我给大家带来简单的UITabeleView用法,估计刚开始有点难懂,多练习就行了。




#import "ViewController.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>//数据源代理协议方法 和代理协议方法


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    [self createTabView];

    

    

}


-(void)createTabView

{

    

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

    

    

    tableView.dataSource = self;

    tableView.delegate = self;

    

    tableView.rowHeight = 60;

    tableView.sectionHeaderHeight = 50;

    tableView.sectionFooterHeight = 40;

    

    //设置头表视图

    UIView *headerView = [[UIView alloc]init];

    headerView.frame = CGRectMake(50, 50, 50, 50);

    headerView.backgroundColor = [UIColor greenColor];

    tableView.tableFooterView = headerView;

    

    //设置尾表视图

    UIView *footerView = [[UIView alloc]init];

    footerView.frame = CGRectMake(50, 50, 50, 0);

    

    footerView.backgroundColor = [UIColor greenColor];

    tableView.tableFooterView = footerView;

    

   

    [self.view addSubview:tableView];

    

    

}



//设置组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 10;

}

//设置行

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

{

    return 5;

}


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

{

    

    

    static int count = 0;

    

    count++;

    NSString *cellID = @"cellID";

    //tableview的复用队列取用cellid标识的可复用的cell

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    

    

    if (cell == nil) {

        static int count1 = 0;

        count1++;

        

        

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

        

        

        //Set the selected cell view

        

        UIView *selView = [[UIView alloc]init];

        selView.backgroundColor = [UIColor cyanColor];

        cell.selectedBackgroundView = selView;

        

    }

    

    if (indexPath.row %2) {

        cell.backgroundColor = [UIColor orangeColor];

        

    }

    else

    {

        cell.backgroundColor = [UIColor purpleColor];

    }

    //当前组合行 tableview上要显示的数据

    

    NSString *string = [NSString stringWithFormat:@"%ld%ld",indexPath.section,indexPath.row];

    

    

    

    //Set the display content on the cell

    cell.textLabel.text =string;

    

    

    

    return cell;

    

    


    

    

}


//Sets the head title specified group

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    return [NSString stringWithFormat:@"%ld组头部",section];

}

//返回指定分组的尾部标题

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

    return [NSString stringWithFormat:@"%ld组尾部",section];

}



//返回指定位置的行高,默认行高为44

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

{

    

    if (indexPath.row %2) {

        return 44;

    }

    else

    {

        return 66;

    }

}


//返回每个分组的头部高度

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section

{

    return 40;

}


-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section

{

    return 20;

}



//返回指定分组的头部视图

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

{

    

    UILabel *label  = [[UILabel alloc]init];

    

    label.text = [NSString stringWithFormat:@"%ld组头部",section];

    label.textColor = [UIColor redColor];

    label.font = [UIFont systemFontOfSize:22];

    return label;

}



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


{

    

    

    UILabel *label = [[UILabel alloc]init];

    label.text = [NSString stringWithFormat:@"%ld组尾部",section];

    label.textColor = [UIColor redColor];

    label.backgroundColor = [UIColor grayColor];

    label.font = [UIFont systemFontOfSize:22];

    return label;

    

}


@end


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值