由於UITableView的Plain模式時,頭部會懸停在界面上,當你需要headerView跟隨着UITableView一起移動時,最好還是使用Grouped。雖然有使用UIScroViewDelegate的方法實現Plain模式下不懸停,
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat sectionHeaderHeight = 40; 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); } }
但是,是設置contentInset畢竟還是有缺陷,而且沒法消除尾部的懸停。所以根據設計的需求,還是使用了Grouped模式。
使用Grouped模式時,出現的問題,section與section之間默認就有空白,直接調用UITableView的dataSource方法,headerForHeight和footerForHeight,return 0是沒有用的,上網找了一下,需要設置
myTableView.sectionHeaderHeight = 0.0
myTableView.sectionFooterHeight = 0.0
先消除section於section之間的空白,再調用 headerForHeight和footerForHeight去返回需要自己定義的headerView和footerView。
最后還發現尾部只用有一段留白,網上各種說
self.automaticallyAdjustsScrollViewInsets = false
self.edgesForExtendedLayout = .None
,但是基本上沒什么卵用,最后用這一句消除了尾部的空白:
tableView.tableFooterView = UIView(frame: CGRectMake(0,0,self.view.width(),0.001))
或者,在footerForHeight返回return 0.001,返回0是沒用的,只有return 0.001
以上測試為XCODE 7.1 , IOS 8.0以上