知識點總結(jié)19:Tableview重要屬性總結(jié)

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // tableView和tableViewController創(chuàng)建時可以指定樣式,默認是plain樣式,我們也可以根據(jù)需要創(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è)置整個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;
    
    // 設(shè)置tableview各組的組頭部和組尾部的高(代理方法可以指定某些組的頭尾部的高度,如果設(shè)置的高度沒有效果,就用代理方法,代理方法比較準確)
//    tableView.sectionHeaderHeight
//    tableView.sectionFooterHeight
    
    
    // group樣式下的tableView頂部如果沒有設(shè)置tableView表頭或者組表頭,則會下移35,即{{0, 35}, {width, height}},如果設(shè)置了組表頭則不會,這是默認設(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.確定重用標識
    static NSString *ID = @"cell";
    // 2.從緩存池中取(沒有注冊)
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 3.如果空就手動創(chuàng)建,指定樣式和重用標識
    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各組的尾部視圖 >(優(yōu)先于)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各組的頭部視圖 >(優(yōu)先于)tableView各組的頭部標題
- (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;
//}

// 對viewForFooterInSection效果顯著,但對titleForFooterInSection并不感冒(位置會偏移),所以推薦頭部尾部都用UIview來設(shè)置
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    // 判斷某一組的高度特殊設(shè)置
    if (section == 1) {
        return 50;
    }
    
    return 10;
}


// 對viewForHeaderInSection效果顯著,但對titleForHeaderInSection并不感冒,所以推薦頭部尾部都用UIview來設(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];
    // 通過觀察小面包可知,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{
    // 因為是封裝起來,所以不是調(diào)用super的方法
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    /*注意點二:*/
    // 不用設(shè)置tableView的代理,也不用遵守協(xié)議,因為本來就是tableView控制器
    // 用代理方法設(shè)置高度優(yōu)先于用屬性self.tableView.sectionHeaderHeight,但是代理方法設(shè)置高度為0會出現(xiàn)異常,如果統(tǒng)一設(shè)置高度為0的話,不能采用代理方法,代理方法只能設(shè)置不為0的情況
    // 當代理方法設(shè)置的高度為0的時候,就會執(zhí)行sectionHeaderHeight或者sectionFooterHeight的值,當代理方法的值不為0時,則優(yōu)先使用代理,代理為0時則會采用sectionHeaderHeight或者sectionFooterHeight的值
    // 當sectionHeaderHeight或者sectionFooterHeight有一個為0時,tableView就會變成默認樣式,第一個cell的frame為{{0, 35}, {320, 44}}
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 10;

    /*注意點三:*/
    // Grouped樣式,沒有設(shè)置表頭或者組頭的高度,則會默認下移35,不過可以通過調(diào)整contenInset來調(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的標識(寫在上面全局變量,不用重復創(chuàng)建)
    
    // 2.從緩存池中取cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        // 創(chuàng)建cell,并指定樣式和添加重用標識
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.section];
    
    return cell;
}

// 設(shè)置高度為0就會失效,所以不能設(shè)置組高度為0
// 用代理方法設(shè)置高度優(yōu)先于用屬性self.tableView.sectionHeaderHeight,但是代理方法設(shè)置高度為0會出現(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會出現(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);
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容