最近項目中有個地方需要用到去掉tableView組頭的粘滯性的需求,于是樓主就開始google解決這個問題了,首先找到了一個代碼段如下:
- (void)scrollViewDidScroll:(UIScrollView )scrollView{
/* 修改成自己的組頭高度就可以了 /
CGFloat sectionHeight = self.height;
if (scrollView == self.tableView) {
if (scrollView.contentOffset.y <= sectionHeight && scrollView.contentOffset.y >= 0)`
{
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
}else{
if(scrollView.contentOffset.y >= sectionHeight)
{
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeight, 0, 0, 0);
}
}
}
}
這是在網上能找到的最多的關于去掉UITableView的組頭的方式,但是這段代碼在我的項目中使用的時候出了點問題,樓主需要實現的是一個有表頭視圖且第一個分組沒有組頭視圖的tableView的展示,在使用時出現了在大于一個分組的時候,有時候向上只能拖動sectionHeight的高度,然后tableView就死活滑不上去了,但是,但是,但是,在樓主之前的demo中是沒有任何問題的,
返回組頭和組尾視圖的代理方法
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height
返回組頭和組尾視圖高度的代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
我們只需要在上面代理方法中返回我們需要顯示的組頭視圖或者組尾視圖就可以了,就可以不用實現scrollViewDidScroll的代理方法來實現取消組頭或者組尾視圖的粘滯性了,因為Grouped方式的tableView默認組頭和組尾視圖隨著tableView一起向上移動的,到此樓主所需的功能已經實現取消組頭的粘滯性的需求,且沒有實現UIScrollow的那個代理方法。
Demo地址:https://github.com/PSSDeveloper/bugDemo/tree/master
最后總結一下:實現取消組頭或者組尾的粘滯性的需求時,可以先考慮使用Grouped方式的tableView,并通過系統的代理方法設置組頭或者組尾視圖,和組頭和組尾的高度。