【iOS】TableView的footerView不隨cell滾動而停留在tableView底部的問題

來源:互聯網
上載者:User

標籤:with   uitable   elf   ini   delegate   group   tab   ids   code   


蘋果官方給我提供TableView的FooterView和HeaderView停留在頂部的非常不錯效果,有時候我們不須要這些FooterView和HeaderView停留在底部或者上部,如今就以FooterView為例,說一下我開發中的解決方案:(眼下兩種)


第一種方法:在初始化TableView時將TableView的樣式設定為UITableViewStylePlain,同一時候重寫tableView的scrollViewDidScroll:(UIScrollView *)scrollView 

詳細代碼例如以下:

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];</span>

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{        CGFloat sectionHeaderHeight = 10;//設定你footer高度    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);    }    }</span>

另外一種方法:在初始化TableView時將TableView的樣式設定為UITableViewStyleGrouped,同一時候在UITableView的代理方法中,將FooterView的高度設定為一個很小(不留下可見空隙)的值,就好啦!(這裡注意,不能設定為0,否者tableView會覺得你沒有設高度就會採取預設高度)

詳細代碼:

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];    tableView.delegate = self;    tableView.dataSource = self;</span>

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{    return 0.0001f;}



【iOS】TableView的footerView不隨cell滾動而停留在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.