【问题标题】:Scroll UITableView so that the header isn't visible滚动 UITableView 使标题不可见
【发布时间】:2010-11-07 23:50:05
【问题描述】:

我有一个带有 UISearchBar 作为 tableViews.tableHeaderView 的 UITableView。就像 3.0 中新的 Mail.app、Notes.app 等一样。我想隐藏 SearchBar,直到用户将它拖到他的视线中。

我的尝试仅在 tableView 中有几个项目时才有效,因此 tableView 实际上想要滚动。我在 loadView 中调用它:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self._tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

尽管如此,Apple 似乎以不同的方式处理此类搜索栏。拖出搜索栏后,它似乎不再绑定到表格单元(在 Notes.app 中,而不是在 Mail.app 中)。

但也许 Apple 对新的 3.0 行为有一种独特的方法,而我就是找不到?

【问题讨论】:

标签: iphone search uitableview scroll uisearchbar


【解决方案1】:

或许你可以这样试试……

[self.tableView setContentOffset:CGPointMake(0,40)];

【讨论】:

  • 每次重新加载表格视图后都需要这样做。 [self.tableView reloadData];
  • 当行数小于适合屏幕的总行数时不起作用。这种情况有什么已知的解决方法吗?
  • 有人对@Zorayr 的评论有答案吗?
【解决方案2】:

也为我工作。我使用了以下内容:

[self.tableView setContentOffset:CGPointMake(0, self.searchDisplayController.searchBar.frame.size.height) animated:NO];

查询搜索栏的高度。

【讨论】:

  • 可爱又简单,虽然我添加了动画:YES 参数,以改善事物的外观。
  • 完美!只需在 viewWillAppear 中添加它,我就得到了我想要的结果。
【解决方案3】:

这个让你获得与 iPod.app 完全相同的行为:

- (void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];

 CGFloat searchBarHeight = CGRectGetHeight([[[self searchDisplayController] searchBar] frame]);
 if ([[self tableView] contentOffset].y < searchBarHeight)
  [[self tableView] setContentOffset:CGPointMake(0, searchBarHeight)];
}

【讨论】:

    【解决方案4】:

    这对我有用。

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.tableView.bounces = YES;
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        [self.tableView setContentOffset:CGPointMake(0, 44)];
    }
    

    【讨论】:

      【解决方案5】:

      我必须先滚动到顶部,然后将 setContentOffset 滚动到 0,然后 searchBar 将可见:

      self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
      self.tableView.setContentOffset(CGPointMake(0, 0), animated: false)
      

      【讨论】:

        【解决方案6】:

        我有点喜欢这样做:

        - (void)viewWillAppear:(BOOL)animated {
            [super viewWillAppear:animated];
        
            // Hide the table view header by default.
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:NO];
        }
        

        这样您就不必担心标题的高度。它只是工作!

        【讨论】:

        • 如果表格视图没有加载行或有零行,解决方案会导致应用崩溃。
        • 哦,明白了。感谢您的评论 Zorayr
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-14
        • 2012-04-05
        • 1970-01-01
        相关资源
        最近更新 更多