UITableView---分组

UITableView分组,该例题分为5个组,每一组有5行,每行内容为当前组下标和行下标,有头标题(HAHA),无尾标题。

显示效果为:


代码如下:


#import "ViewController.h"


//遵守协议

@interface ViewController () <UITableViewDelegate,UITableViewDataSource>


//UITableView

@property (nonatomic,weak)UITableView * tableView;


//分组头数据

@property (nonatomic,strong) NSMutableArray * sectionArray;




//strong ---数据类型

//weak   ---控件之类的



@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    //加载数据

    [self_loadData];

    

    UITableView * table=[[UITableViewalloc]initWithFrame:CGRectMake(0,20, self.view.frame.size.width,self.view.frame.size.height-20)style:UITableViewStyleGrouped];

    table.delegate=self;

    table.dataSource=self;

    

   self.tableView=table;

    table.sectionFooterHeight=0;  //如果有代理的话,此行没有效果,以代理为先

    //table.sectionHeaderHeight=100;

    [self.viewaddSubview:table];

    

    

}


#pragma mark - 加载数据

- (void) _loadData

{

    NSArray * array=@[@"分组1",@"分组2",@"分组3",@"分组4",@"分组5"];

   self.sectionArray=[NSMutableArrayarrayWithArray:array];

   

    

}




#pragma mark - UITableViewDatasource

//返回行数

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

{

    //return 3;  //在该分组中有3

    //return (section+1)*2;    //下标为0的分组有2

                            //下标为1的分组有4

                            //下标为2的分组有6

                            //下标为3的分组有8

  

    returnself.sectionArray.count;

    

    

}


//返回cell

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

{

   static NSString * identy=@"table";

    UITableViewCell * cell=[tableViewdequeueReusableCellWithIdentifier:identy];

   if (!cell)

    {

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identy];

    }

    cell.textLabel.text = [NSStringstringWithFormat:@"%li %li",indexPath.section,indexPath.row]; //打印分组和行数


   return cell;

    

}



#pragma mark - 返回分组数量

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView

{

    returnself.sectionArray.count;  //分组数组长度

    //return 4;   //分组有4

}


#pragma mark - UITableViewDelegate

//设置分组的头标题

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

{

    return@"haha";

}



//设置分组的尾标题

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

//{

//    return @"hello world";

//}


#pragma mark - 自定义头部

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

//{

//    UIView * view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];

//    view.backgroundColor=[UIColor redColor];

//    return view;

//}


#pragma mark - 自定义尾部

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

//{

//    UIView * view=[[UIView alloc]init];

//    view.backgroundColor=[UIColor blueColor];

//    return  view;

//}


#pragma mark - 设置头部的高度

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

{

   return 50;

}



#pragma mark - 设置尾部的高度

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

//{

//    return 30;

//}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    

}


@end



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值