知識(shí)點(diǎn)總結(jié)19:Tableview重要屬性總結(jié)

#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);
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市嘿期,隨后出現(xiàn)的幾起案子咽笼,更是在濱河造成了極大的恐慌娜遵,老刑警劉巖俘侠,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異种玛,居然都是意外死亡曹洽,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)蹭沛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)臂寝,“玉大人,你說(shuō)我怎么就攤上這事摊灭∨乇幔” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵帚呼,是天一觀的道長(zhǎng)掏缎。 經(jīng)常有香客問(wèn)我,道長(zhǎng)萝挤,這世上最難降的妖魔是什么御毅? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮怜珍,結(jié)果婚禮上端蛆,老公的妹妹穿的比我還像新娘。我一直安慰自己酥泛,他們只是感情好今豆,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著柔袁,像睡著了一般呆躲。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上捶索,一...
    開(kāi)封第一講書(shū)人閱讀 51,182評(píng)論 1 299
  • 那天插掂,我揣著相機(jī)與錄音,去河邊找鬼。 笑死辅甥,一個(gè)胖子當(dāng)著我的面吹牛酝润,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播璃弄,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼要销,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了夏块?” 一聲冷哼從身側(cè)響起疏咐,我...
    開(kāi)封第一講書(shū)人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎脐供,沒(méi)想到半個(gè)月后浑塞,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡患民,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年缩举,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片匹颤。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡仅孩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出印蓖,到底是詐尸還是另有隱情辽慕,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布赦肃,位于F島的核電站溅蛉,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏他宛。R本人自食惡果不足惜船侧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望厅各。 院中可真熱鬧镜撩,春花似錦、人聲如沸队塘。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)憔古。三九已至遮怜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鸿市,已是汗流浹背锯梁。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工即碗, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人涝桅。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓拜姿,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親冯遂。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容