使用UITableView的分組樣式


 

分組樣式顧名思義是對TableView中的數據行進行分組處理,每個分組都有一個header和footer。

TableView中header的英文文本是大寫的,footer的英文文本是小寫的。如下圖淺灰色區域就是header和footer。

header的作用更像是標題,而footer則是詳細描述信息

 

 

 

在之前的文章中我們創建的TableView樣式是UITableViewStylePlain,分組樣式可以使用UITableViewStyleGrouped創建:

 

設置UITableView的header和footer的文本

UITableViewDataSource中有兩個方法可以幫助我們設置header和footer的本文,它們分別是:

  - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

  - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

 

下面我們實現這兩個方法:

 

運行效果:

 

自定義Header和Footer的樣式(view)

通過下面幾個在UITableViewDelegate協議中聲明的方法,我們可以對Header和Footer進行自定義操作。

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

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

 

方法實現:

作為演示,只是簡單設置了一個UIView作為header和footer的視圖,

 

運行效果:

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM