#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// tableView和tableViewController創(chuàng)建時(shí)可以指定樣式,默認(rèn)是plain樣式,我們也可以根據(jù)需要?jiǎng)?chuàng)建Group樣式(中間有間隔)
// UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
// tableView一共三大部分,表頭,表尾,內(nèi)容,而內(nèi)容又包括各組cell,各組cell都有組頭部和組尾部
// tableView和tableViewCell都有樣式,前者有2種,后者有4種
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped];
tableView.backgroundColor = [UIColor purpleColor];
tableView.delegate = self;
tableView.dataSource = self;
// 設(shè)置屬性的代理方法優(yōu)先于屬性方法
// 設(shè)置tableView的所有cell的高(代理方法可以指定某些cell或者某組特定高度)
// tableView.rowHeight = 200;
// 設(shè)置整個(gè)tableview的表頭和表尾的view(沒(méi)有代理方法,因?yàn)楸眍^表尾部就只有各一個(gè),高度由frame決定)
// tableView.tableHeaderView
// tableview.tableFooterView
// UIView * view = [[UIView alloc] init];
// view.backgroundColor = [UIColor redColor];
// view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50);
// tableView.tableHeaderView = view;
// 設(shè)置tableview各組的組頭部和組尾部的高(代理方法可以指定某些組的頭尾部的高度,如果設(shè)置的高度沒(méi)有效果,就用代理方法,代理方法比較準(zhǔn)確)
// tableView.sectionHeaderHeight
// tableView.sectionFooterHeight
// group樣式下的tableView頂部如果沒(méi)有設(shè)置tableView表頭或者組表頭,則會(huì)下移35,即{{0, 35}, {width, height}},如果設(shè)置了組表頭則不會(huì),這是默認(rèn)設(shè)置
// tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
[self.view addSubview:tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 1.確定重用標(biāo)識(shí)
static NSString *ID = @"cell";
// 2.從緩存池中取(沒(méi)有注冊(cè))
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3.如果空就手動(dòng)創(chuàng)建,指定樣式和重用標(biāo)識(shí)
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.section];
return cell;
}
// tableView各組的組頭標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"我是tableView組頭標(biāo)題--組頭部";
}
// tableView各組的尾部標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"我是tableView組尾標(biāo)題---組尾部";
}
//// tableView各組的尾部視圖 >(優(yōu)先于)tableView各組的尾部標(biāo)題
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
// UIView * view = [[UIView alloc] init];
// view.backgroundColor = [UIColor redColor];
// view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 5);
// return view;
//}
// tableView各組的頭部視圖 >(優(yōu)先于)tableView各組的頭部標(biāo)題
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * view = [[UIView alloc] init];
view.backgroundColor = [UIColor yellowColor];
// 這里設(shè)置frame并不能控制其大小和位置
// view.frame = CGRectMake(0, 10, 20, 100);
return view;
}
// 給定預(yù)設(shè)高度
//- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
// return 5;
//}
// 對(duì)viewForFooterInSection效果顯著,但對(duì)titleForFooterInSection并不感冒(位置會(huì)偏移),所以推薦頭部尾部都用UIview來(lái)設(shè)置
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
// 判斷某一組的高度特殊設(shè)置
if (section == 1) {
return 50;
}
return 10;
}
// 對(duì)viewForHeaderInSection效果顯著,但對(duì)titleForHeaderInSection并不感冒,所以推薦頭部尾部都用UIview來(lái)設(shè)置
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// 判斷某一組的高度特殊設(shè)置
// 第0組不顯示組部視圖
if (section == 0) {
return 20;
}
if (section == 1) {
return 20;
}
return 0;
}
// 給cell自定義高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// 判斷某一組的高度特殊設(shè)置
if (indexPath.section == 1) {
return 50;
}
return 20;
}
// 研究group樣式下的tableView頂部下移35,即{{0, 35}, {width, height}}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// 通過(guò)觀察小面包可知,tableView里面有個(gè)wrappedView,里面放著所有的cell
NSLog(@"點(diǎn)中cell的frame----%@", NSStringFromCGRect(cell.frame));
}
@end
需要特別注意的是
#import "ZGKMeViewController.h"
#import "ZGKSettingViewController.h"
#import "ZGKBarButtonItemManager.h"
static NSString * const ID = @"cell";
@interface ZGKMeViewController () // <UITableViewDelegate, UITableViewDataSource>
@end
@implementation ZGKMeViewController
// 重寫(xiě)init方法,將Group樣式封裝起來(lái)
/*注意點(diǎn)一:*/
- (instancetype)init{
// 因?yàn)槭欠庋b起來(lái),所以不是調(diào)用super的方法
return [self initWithStyle:UITableViewStyleGrouped];
}
- (void)viewDidLoad {
[super viewDidLoad];
/*注意點(diǎn)二:*/
// 不用設(shè)置tableView的代理,也不用遵守協(xié)議,因?yàn)楸緛?lái)就是tableView控制器
// 用代理方法設(shè)置高度優(yōu)先于用屬性self.tableView.sectionHeaderHeight,但是代理方法設(shè)置高度為0會(huì)出現(xiàn)異常,如果統(tǒng)一設(shè)置高度為0的話,不能采用代理方法,代理方法只能設(shè)置不為0的情況
// 當(dāng)代理方法設(shè)置的高度為0的時(shí)候,就會(huì)執(zhí)行sectionHeaderHeight或者sectionFooterHeight的值,當(dāng)代理方法的值不為0時(shí),則優(yōu)先使用代理,代理為0時(shí)則會(huì)采用sectionHeaderHeight或者sectionFooterHeight的值
// 當(dāng)sectionHeaderHeight或者sectionFooterHeight有一個(gè)為0時(shí),tableView就會(huì)變成默認(rèn)樣式,第一個(gè)cell的frame為{{0, 35}, {320, 44}}
self.tableView.sectionHeaderHeight = 0;
self.tableView.sectionFooterHeight = 10;
/*注意點(diǎn)三:*/
// Grouped樣式,沒(méi)有設(shè)置表頭或者組頭的高度,則會(huì)默認(rèn)下移35,不過(guò)可以通過(guò)調(diào)整contenInset來(lái)調(diào)整間距
self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 1.確定cell的標(biāo)識(shí)(寫(xiě)在上面全局變量,不用重復(fù)創(chuàng)建)
// 2.從緩存池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
// 創(chuàng)建cell,并指定樣式和添加重用標(biāo)識(shí)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.section];
return cell;
}
// 設(shè)置高度為0就會(huì)失效,所以不能設(shè)置組高度為0
// 用代理方法設(shè)置高度優(yōu)先于用屬性self.tableView.sectionHeaderHeight,但是代理方法設(shè)置高度為0會(huì)出現(xiàn)異常,如果統(tǒng)一設(shè)置高度為0的話,還是不要采用代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// if (section == 1) {
// return 1;
// }
return 0;
}
// 用代理方法設(shè)置高度優(yōu)先于用屬性self.tableView.sectionHeaderHeight,但是代理方法設(shè)置高度為0會(huì)出現(xiàn)異常,如果統(tǒng)一設(shè)置高度為0的話,還是不要采用代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc] init];
view.backgroundColor= [UIColor yellowColor];
return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIView alloc] init];
view.backgroundColor= [UIColor redColor];
return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 拿到選中的cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"選中cell的frame:%@------header:%f -----footer:%f", NSStringFromCGRect(cell.frame), tableView.sectionHeaderHeight, tableView.sectionFooterHeight);
}