如何讓 UITableView 的 headerView跟隨 cell一起滾動

來源:互聯網
上載者:User

                                                         如何讓 UITableView 的 headerView跟隨 cell一起滾動

 

        在我們利用 UITableView 展示我們的內容的時候,我需要在頂部放一個不同於一般的cell的 介面,這個介面比較獨特。

 

        1。 所以我就把它 作為一個section的 headerView。

 

        也就是在函數:

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

 

        裡面返回 這個UIView。

 

 

        但是,由於這個UIView占的空間很大,基本佔用整個螢幕的高度,而滾動tableView的時候,只滾動cell的內容,而這個section的

headerView卻不跟著滾動。

 

 

        後面,我想出了方法2。

 

 

       2。  設定 tableView的 style為  UITableViewStyleGrouped,然後讓

          - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

       返回1。

 

        這樣確實可以讓 headerView ,在滾動tableView的時候,跟隨著cell的內容一起滾動。但是,我發現,下面的cell都被加上了邊框,

        而且cell的水平顯示範圍變窄了。

 

        所以嘗試了方法3。

 

 

 

       3。  將UIView設定為  整個tableView的headerView,而不是 section 0的headerView

 

              self.tableView.tableHeaderView=header;

 

             這樣,就可以完美的滿足 headerView跟隨cell的內容一起滾動的要求拉。

 

 

 

     結論:設定 UIView為  tableView的tableHeaderView即可實現 headerView跟隨tableView一起滾動的效果。

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.