UITableView的分组示例及常用方法

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>


/**  UITableView*/

@property(nonatomic,weak) UITableView * tableView;


/**  分组头数据*/

@property(nonatomic,strong) NSMutableArray * sectionArray;


/**  数据数组*/

@property(nonatomic,strong) NSArray * dataArray;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    //加载数据

    [self _loadData];

    

    UITableView * table=[[UITableView alloc] 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 = 30;

    [self.view addSubview:table];

    

    

}


//加载数据

-(void)_loadData{


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

    self.sectionArray=[NSMutableArray arrayWithArray:array];

    

    NSString * path=[[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil];

    self.dataArray=[NSArray arrayWithContentsOfFile:path];

    

}



#pragma mark - UITableViewDatasource

//返回行数

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

    

    NSDictionary * temDic=self.dataArray[section];

    NSArray * temArray=temDic[@"friends"];

    return temArray.count;

}


//返回cell

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

    static NSString * identy=@"table";

    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy];

    if (!cell) {

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

    }

    

    NSDictionary * temDic=self.dataArray[indexPath.section];

    NSArray * temArray=temDic[@"friends"];

    cell.textLabel.text=temArray[indexPath.row];

    return cell;


}


//返回分组数量

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

    return self.dataArray.count;

}




#pragma mark - UITableViewDelegate

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

    NSDictionary * temDic=self.dataArray[section];

   return temDic[@"group"];

}


//设置尾部的标题,即使footer高度设置为0,也会显示

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

//return @"hello world";

//}



#pragma mark * 自定义头部  自定义的优先级高,即使footer高度设置为0,也会显示(代理优先)

//- (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 heightForHeaderInSection:(NSInteger)section{

    return 30;

}


#pragma mark * 设置尾部的高度

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

//    return 30;

//}



//设置右侧的索引

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    

    NSMutableArray * array=[NSMutableArray array];

    for (NSDictionary *dic  in self.dataArray) {

        [array addObject:dic[@"group"]];

    }

    

    return array;

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值