需求:
- UITableView 右侧有索引
- sectionHeader禁止悬浮
3.点击索引,sectionHeader在最上方正常显示
解决方案
- UITableView 右侧有索引 所以只能选择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.重写完后发现,点右侧索引,sectionHeader是不显示的,只显示第一个row内容
解决: 找到点击索引方法,增加标示,告知scrollView不需要更改contentInset
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
_doNotChangeOffSet = YES;
// 获取所点目录对应的indexPath值
NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
// 让table滚动到对应的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);
}
}
}
完美解决。