iOS UITableView 的 Plain和Grouped樣式的區(qū)別

官方文檔寫(xiě)的非常經(jīng)典:

Table views can have one of two styles, UITableViewStylePlain and UITableViewStyleGrouped. When you create a UITableView instance you must specify a table style, and this style cannot be changed.

In the plain style, section headers and footers float above the content if the part of a complete section is visible. A table view can have an index that appears as a bar on the right hand side of the table (for example, "A" through "Z"). You can touch a particular label to jump to the target section.

The grouped style of table view provides a default background color and a default background view for all cells. The background view provides a visual grouping for all cells in a particular section. For example, one group could be a person's name and title, another group for phone numbers that the person uses, and another group for email accounts and so on. See the Settings application for examples of grouped tables. Table views in the grouped style cannot have an index.

意思是:
tableView有兩種style,UITableViewStylePlain和UITableViewStyleGrouped。當(dāng)你創(chuàng)建一個(gè)UITableView實(shí)例必須指定其的style,并且這種style是不能被改變的。

Plain style的UITableView

在plain style的tableView中,當(dāng)一個(gè)section的rows有一部分可見(jiàn)時(shí),section的header和footer浮動(dòng)在內(nèi)容頂部。plain style的tableView可以有一個(gè)section索引,作為一個(gè)bar在table的右邊(例如A ~ Z)。你可以點(diǎn)擊一個(gè)特定的標(biāo)簽,跳轉(zhuǎn)到目標(biāo)section。例如下圖:


Group style的UITableView

在grouped style的tableView中,所有單元格擁有一個(gè)默認(rèn)的背景顏色和默認(rèn)背景視圖。背景視圖為特定section中的所有cell提供可視分組。例如,一個(gè)group可以是一個(gè)人的名字和標(biāo)題,另一個(gè)group可以是電話,電子郵件帳戶等??蓞⒖糹phone“設(shè)置”程序。
注意:在grouped style表中不能有一個(gè)(右邊的)索引。如下圖:

Group類型默認(rèn)設(shè)置tableView灰色背景色,cell為白色背景色,section外邊緣設(shè)置淺灰色邊框,cell設(shè)置淺灰色間隔線。如下圖:


區(qū)別總結(jié):

一、UITableViewStylePlain

1.plain類型有多段時(shí),段頭停留(自帶效果)
2.plain類型默認(rèn)section之間沒(méi)有中間的間距和頭部間距(想讓plain類型的section之間留有空白,需要在UITableView代理方法中return自定義的header和footer,并在自定義的UITableViewHeaderFooterView里面重寫(xiě)setFrame方法)

  1. 擴(kuò)展:讓plain類型的UITableView段頭不停留(取消粘性效果)
(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 30;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
二、UITableViewStyleGroup
注意:去掉Group類型的表section頭部和中間間隔的方法:

1.設(shè)置標(biāo)題tableHeaderView的高度為特小值,但不能為零,若為零的話,ios會(huì)取默認(rèn)值18,就無(wú)法消除頭部間距了。

      UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)];
      view.backgroundColor = [UIColor redColor];
      self.tableView.tableHeaderView = view;

2.設(shè)置代理方法(中間的留白其實(shí)是段尾的高度,代理的作用設(shè)置段尾的高度,返回值也不能為0,否則系統(tǒng)啟用默認(rèn)值18)

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
      return 0.01f;
}
//特殊的處理方法也能實(shí)現(xiàn)該效果
self.tableView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0);

3.自定義類繼承UITableViewHeaderFooterView,重寫(xiě)setFrame方法,在UITableView代理方法中return 自定義類創(chuàng)建的section的header和footer。

-(void)setFrame:(CGRect)frame{
frame.size.height+=10;
[super setFrame:frame];
}

注意:sectionHeaderHeight/sectionFooterHeight這2個(gè)屬性只在Grouped類型,且未實(shí)現(xiàn)代理方法tableView:heightForHeaderInSection: 時(shí)有效,在plain風(fēng)格下設(shè)置無(wú)效。故在使用UITableView過(guò)程中盡量使用代理方法設(shè)置sectionHeader和sectionFooter的高度。

注: 部分轉(zhuǎn)載http://www.itdecent.cn/p/3a5063993368

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

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

  • 一、簡(jiǎn)介 官方給出了比較全面的介紹,要點(diǎn)摘錄如下: table view的作用:導(dǎo)航、展示索引列表、展示詳情信息、...
    quantiza閱讀 820評(píng)論 0 1
  • 概述在iOS開(kāi)發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,289評(píng)論 3 38
  • 那些愛(ài)情沒(méi)有教你的事 文|顧沁 就算與時(shí)間為敵就算與全世界背離 ----獻(xiàn)給我的摯友 = 1 = 姜冉冉是個(gè)愛(ài)笑的...
    顧沁小姐閱讀 330評(píng)論 1 4
  • 工作臺(tái) 一般情況下,我們需要一個(gè)“地方“,在這個(gè)“地方”,我們能一站式項(xiàng)目管理,問(wèn)題跟蹤,版本查看,wiki服務(wù),...
    randyjia閱讀 1,093評(píng)論 0 1
  • 一說(shuō)到人際交往,大多數(shù)的人都會(huì)頭疼。特別是剛開(kāi)始要去做銷售的。 我自己有時(shí)候就會(huì)想:真的要經(jīng)營(yíng)那么多的人際關(guān)系嗎?...
    帶貓行走的羊閱讀 277評(píng)論 0 0

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