#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// tableView和tableViewController創建時可以指定樣式,默認是plain樣式,我們也可以根據需要創建Group樣式(中間有間隔)
// UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
// tableView一共三大部分,表頭,表尾,內容,而內容又包括各組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;
// 設置屬性的代理方法優先于屬性方法
// 設置tableView的所有cell的高(代理方法可以指定某些cell或者某組特定高度)
// tableView.rowHeight = 200;
// 設置整個tableview的表頭和表尾的view(沒有代理方法,因為表頭表尾部就只有各一個,高度由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;
// 設置tableview各組的組頭部和組尾部的高(代理方法可以指定某些組的頭尾部的高度,如果設置的高度沒有效果,就用代理方法,代理方法比較準確)
// tableView.sectionHeaderHeight
// tableView.sectionFooterHeight
// group樣式下的tableView頂部如果沒有設置tableView表頭或者組表頭,則會下移35,即{{0, 35}, {width, height}},如果設置了組表頭則不會,這是默認設置
// 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.確定重用標識
static NSString *ID = @"cell";
// 2.從緩存池中取(沒有注冊)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3.如果空就手動創建,指定樣式和重用標識
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.section];
return cell;
}
// tableView各組的組頭標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"我是tableView組頭標題--組頭部";
}
// tableView各組的尾部標題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"我是tableView組尾標題---組尾部";
}
//// tableView各組的尾部視圖 >(優先于)tableView各組的尾部標題
//- (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各組的頭部視圖 >(優先于)tableView各組的頭部標題
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * view = [[UIView alloc] init];
view.backgroundColor = [UIColor yellowColor];
// 這里設置frame并不能控制其大小和位置
// view.frame = CGRectMake(0, 10, 20, 100);
return view;
}
// 給定預設高度
//- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
// return 5;
//}
// 對viewForFooterInSection效果顯著,但對titleForFooterInSection并不感冒(位置會偏移),所以推薦頭部尾部都用UIview來設置
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
// 判斷某一組的高度特殊設置
if (section == 1) {
return 50;
}
return 10;
}
// 對viewForHeaderInSection效果顯著,但對titleForHeaderInSection并不感冒,所以推薦頭部尾部都用UIview來設置
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// 判斷某一組的高度特殊設置
// 第0組不顯示組部視圖
if (section == 0) {
return 20;
}
if (section == 1) {
return 20;
}
return 0;
}
// 給cell自定義高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// 判斷某一組的高度特殊設置
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];
// 通過觀察小面包可知,tableView里面有個wrappedView,里面放著所有的cell
NSLog(@"點中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
// 重寫init方法,將Group樣式封裝起來
/*注意點一:*/
- (instancetype)init{
// 因為是封裝起來,所以不是調用super的方法
return [self initWithStyle:UITableViewStyleGrouped];
}
- (void)viewDidLoad {
[super viewDidLoad];
/*注意點二:*/
// 不用設置tableView的代理,也不用遵守協議,因為本來就是tableView控制器
// 用代理方法設置高度優先于用屬性self.tableView.sectionHeaderHeight,但是代理方法設置高度為0會出現異常,如果統一設置高度為0的話,不能采用代理方法,代理方法只能設置不為0的情況
// 當代理方法設置的高度為0的時候,就會執行sectionHeaderHeight或者sectionFooterHeight的值,當代理方法的值不為0時,則優先使用代理,代理為0時則會采用sectionHeaderHeight或者sectionFooterHeight的值
// 當sectionHeaderHeight或者sectionFooterHeight有一個為0時,tableView就會變成默認樣式,第一個cell的frame為{{0, 35}, {320, 44}}
self.tableView.sectionHeaderHeight = 0;
self.tableView.sectionFooterHeight = 10;
/*注意點三:*/
// Grouped樣式,沒有設置表頭或者組頭的高度,則會默認下移35,不過可以通過調整contenInset來調整間距
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的標識(寫在上面全局變量,不用重復創建)
// 2.從緩存池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
// 創建cell,并指定樣式和添加重用標識
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.section];
return cell;
}
// 設置高度為0就會失效,所以不能設置組高度為0
// 用代理方法設置高度優先于用屬性self.tableView.sectionHeaderHeight,但是代理方法設置高度為0會出現異常,如果統一設置高度為0的話,還是不要采用代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// if (section == 1) {
// return 1;
// }
return 0;
}
// 用代理方法設置高度優先于用屬性self.tableView.sectionHeaderHeight,但是代理方法設置高度為0會出現異常,如果統一設置高度為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);
}