官方文檔寫(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方法)
- 擴(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