2019-01-16 UITableView 索引 sectionHeader禁止懸浮

需求:

  1. UITableView 右側(cè)有索引
  2. sectionHeader禁止懸浮
    3.點擊索引,sectionHeader在最上方正常顯示

解決方案

  1. 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);
        }
    }
}

完美解決。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容