tableView 的UITableViewStyleGrouped類型

原地址: ? ? ?http://www.lxweimin.com/p/6597b51e6098

我們知道使用UITableView的時候有個技巧:使用table.tableFooterView = [UIView new];一行代碼可以解決UITableView在cell比較少的情況下不顯示下面的分割線條How to remove empty cells in UITableView? 。

今天在使用UITableViewStyleGrouped類型的UITableView的時候又發現一個小技巧。

當設置UITableView為UITableViewStyleGrouped的時候,下面兩段代碼將導致不同的界面效果:

方式1:

table = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];

table.backgroundColor = [UIColor? clearColor];

table.separatorColor = kLineColor;

table.tableFooterView = [UIView new];

table.delegate = self;

table.dataSource = self;

方式1的效果圖:

方式1效果圖.png

方式2:

table = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];

table.backgroundColor = [UIColor? clearColor];

table.separatorColor = kLineColor;

table.delegate = self;

table.dataSource = self;

table.tableFooterView = [UIView new];

方式2的效果圖:

方式2的效果圖.png

前提條件:

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section

{

return 10.f;

}

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section

{

return 0.01f;

}

我們看到代碼順序不同直接導致了界面顯示不同。我們看到第一種情況,這什么鬼?

問題1:如何調整第一個section header 的默認高度

我相信肯定有不少人遇到過這種情況---怎么修改UITableViewStyleGrouped類型的UITableView的第一個sction header的默認高度?,然后網上各種搜怎么解決UITableViewStyleGrouped類型的UITableView的第一個section header的高度問題,然后你會搜到這種解決方案:

table.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);通過這行代碼來將UITableView的第一個section header隱藏一部分上去。

通調整可以看到下面的效果,會比之前好多了,但是在實際使用過程中要達到精確的section header 高度,需要多次調整contentInset來看效果,頗為麻煩。

效果圖.png

問題2:如何改變默認section footer 默認高度

在stackoverflow上面會有人告訴你,可以通過以下設置比0稍微大一點點的方式來改變section footer的高度。

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section

{

return 0.01;

}

見:How to change height of grouped UITableView header?

How to hide first section header in UITableView (grouped style)

總結:

在使用UITableViewStyleGrouped類型UITableView的時候,要想去掉頭部默認高度,建議使用以下代碼:

table = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];

table.backgroundColor = [UIColor clearColor];

table.separatorColor = kLineColor;

table.delegate = self;

table.dataSource = self;

table.tableFooterView = [UIView new];

以及:

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section

{

return 10.f;

}

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section

{

return 0.01f;

}

具體為什么table.tableFooterView = [UIView new];與

table.delegate = self;

table.dataSource = self;

順序顛倒會導致UITableViewStyleGrouped類型UITableView的效果造成不一樣的影響,對UITableViewStylePlain類型的UITableView卻沒有什么影響,看蘋果官方文檔中,對tableFooterView也沒有做很詳細的說明,我猜測是UITableViewStyleGrouped類型UITableView在runtime中做了特殊處理。

相比之下,第二種代碼實現的方式更簡單明了,只需要注意代碼順序即可。在前期沒發現第二種實現方法,導致一直按照第一種方式折騰了好久,走了不少彎路,特做此記錄,轉載請注明出處。

2016-09-06更新

感謝 HYY在第七樓分享的更加直接粗暴的方法:

table.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];

來實現去掉UITableViewStyleGrouped類型UITableView頭部高度,但是為了調整分區之間的間距還是需要實現heightForFooterInSection方法的。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容