如果向上滚动,UITableview标题不再显示

时间:2016-05-19 09:54:11

标签: ios objective-c uitableview uisearchbardisplaycontrol

我有UITableview Controller,它在标题中有搜索栏。当我向上滚动表格视图并且它反弹时。但搜索栏隐藏了。当我向下滚动然后显示的搜索栏。任何人都可以告诉我如何再次显示搜索栏?

我尝试了以下代码,但它无法顺利运行:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;//Change as per your table header hight
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        //scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        CGPoint newOffset = CGPointMake(0, -[self.tableView  contentInset].top);
        [self.tableView setContentOffset:newOffset animated:YES];
    }
}

以下是滚动前查看的屏幕截图: enter image description here

这是错误的观点,滚动后:

enter image description here

2 个答案:

答案 0 :(得分:1)

如何禁用动画关闭并放置这样的自定义动画:

也确保使用MAcro具有相同的sectionHeader高度。我仍然怀疑你的评论是35或25。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
            CGFloat sectionHeaderHeight = 40;//Change as per your table header hight
            if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {

    [UIView animateWithDuration: 1.0
                      animations: ^{
                          scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
                      }completion: ^(BOOL finished){
                      }
        ];
            } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
                CGPoint newOffset = CGPointMake(0, -[self.tableView  contentInset].top);


[UIView animateWithDuration: 1.0
                  animations: ^{
                      [self.tableView setContentOffset:newOffset animated:NO];
                  }completion: ^(BOOL finished){
                  }
    ];
            }
}

答案 1 :(得分:0)

试试这段代码

- (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);  
    }  
} 

但问题是,你的tableview标题不是40,它是35或25,这意味着你的tableview无法为插图设置正确的数字,请尝试更改40以更正标题高度。