需求:
- UITableView 右側(cè)有索引
- sectionHeader禁止懸浮
3.點擊索引,sectionHeader在最上方正常顯示
解決方案
- UITableView 右側(cè)有索引 所以只能選擇plain模式不能選擇Grouped
2.plain模式的sectionHeader是懸浮的,滾動時候永遠在最頂端,直到下個sectionHeader顯示到頂端。
解決:重寫scrollView滾動代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.tableView)
{
CGFloat sectionHeaderHeight = 44; //自定義的sectionHeaderHeight
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);
}
}
}
3.重寫完后發(fā)現(xiàn),點右側(cè)索引,sectionHeader是不顯示的,只顯示第一個row內(nèi)容
解決: 找到點擊索引方法,增加標(biāo)示,告知scrollView不需要更改contentInset
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
_doNotChangeOffSet = YES;
// 獲取所點目錄對應(yīng)的indexPath值
NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
// 讓table滾動到對應(yīng)的indexPath位置
[tableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
_doNotChangeOffSet = NO;
return index;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.tableView)
{
if (_doNotChangeOffSet) {
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
return;
}
CGFloat sectionHeaderHeight = 44; //自定義的sectionHeaderHeight
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);
}
}
}
完美解決。