【问题标题】:UITableview header not shown again if scrolled up如果向上滚动,UITableview 标题不会再次显示
【发布时间】:2016-05-19 09:54:10
【问题描述】:

我有 UITableview 控制器,它在标题中有搜索栏。当我向上滚动表格视图并反弹时。但是搜索栏隐藏了。当我向下滚动然后显示搜索栏。谁能告诉我如何再次显示搜索栏?

我尝试了以下代码,但运行不顺利:

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

这是滚动前的屏幕截图:

这是错误的视图,滚动后:

【问题讨论】:

  • 你能展示你的 viewForHeaderInSection 方法吗?
  • 这个标题不是节标题..它的tableview的标题。
  • 这是-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if(section==0) { return 35.0; } else { return 25.0; } }部分的代码
  • 硬编码的 40 是什么?如果你达到 200 会发生什么
  • 它的标题高度正在工作,但我正在 viewdidscroll 中更改它,这就是为什么,它不平滑

标签: ios objective-c uitableview uisearchbardisplaycontrol


【解决方案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){
                  }
    ];
            }
}

【讨论】:

  • 它仍然隐藏在视图后面
  • 我已经评论了那几行但仍然面临同样的问题
  • 你能不能在heightForHeaderInSection的每个地方尝试一下40.0f。
  • 我想出了一个办法,谢谢你的帮助:)
  • 如果您认为它对其他人也有帮助,请投票支持该问题。谢谢
【解决方案2】:

试试这个代码

- (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 更改为正确的标题高度。

【讨论】:

  • 它不是节标题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 2014-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多