【问题标题】:How to prevent scroll the tableHeaderView of UItable view, To stick at the top如何防止在 UItableview 中滚动 tableHeaderView,粘在顶部
【发布时间】:2013-01-20 11:07:53
【问题描述】:

在我的拆分视图应用程序中,无法将搜索栏添加到拆分视图的 rootView

所以我在ui表视图的tableHeaderView中动态添加了搜索栏,如下所示

searchBar = [[UISearchBar alloc] init];
      searchBar.frame=CGRectMake(0, self.tableView.frame.origin.y, self.tableView.frame.size.width, 44);
      [searchBar sizeToFit];
      self.tableView.tableHeaderView = searchBar;

向下滚动时:iThe tableHeaderView 也会向下滚动,因此搜索栏也会滚动

当滚动到顶部时:tableHeaderView 也会滚动到顶部,因此搜索栏也会滚动

我实现了如下代码来解决这个问题 this helps only when scrolls down 但是当我们再次将表格视图滚动到顶部时随表格视图移动

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
      CGRect rect = self.tableView.tableHeaderView.frame;
      rect.origin.y = MIN(0, self.tableView.contentOffset.y);
      self.tableView.tableHeaderView.frame = rect;
}

我需要始终将 tableHeaderView/ 搜索栏粘贴在视图顶部

怎么做

【问题讨论】:

  • 找到解决办法了吗>
  • 我花了很多时间完成这项任务,但仍然没有得到任何解决方案....如果您解决了上述问题,请告诉我...

标签: iphone objective-c uitableview uisplitviewcontroller nstableheaderview


【解决方案1】:

将您的 searchBar 放在单独的视图中,并将该视图放在表格视图上方。这意味着它保持不变。

【讨论】:

    【解决方案2】:

    我确信这个问题之前已经回答过了,但是假设您使用的是UITableViewController,您可以将view 属性设置为任何您想要的属性。因此,一种方法是设置一个容器视图,顶部是搜索栏,下面是表格,并让view 成为这个容器。默认情况下,tableView 返回view,因此您需要注意的另一个细节是覆盖tableView 属性以返回实际的表视图(存储在ivar 中)。代码可能如下所示:

    @synthesize tableView = _tableView;
    
    - (void)loadView
    {
        [super loadView];
    
        _tableView = [super tableView];
    
        // Container for both the table view and search bar
        UIView *container = [[UIView alloc] initWithFrame:self.tableView.frame];
    
        // Search bar
        UIView *searchBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
    
        // Reposition the table view below the search bar
        CGRect tableViewFrame = container.bounds;
        tableViewFrame.size.height = tableViewFrame.size.height - searchBar.frame.size.height;
        tableViewFrame.origin.y = searchBar.frame.size.height + 1;
        self.tableView.frame = tableViewFrame;
    
        // Reorganize the view heirarchy
        [self.tableView.superview addSubview:container];
        [container addSubview:self.tableView];
        [container addSubview:searchBar];
        self.view = container;
    }
    

    【讨论】:

      【解决方案3】:

      你可以添加tabBar和tableView分开

      mySearchBar = [[UISearchBar alloc] init];
      [mySearchBar setHidden:NO];
      mySearchBar.placeholder = @"Search item here";
      mySearchBar.tintColor = [UIColor darkGrayColor];
      mySearchBar.frame = CGRectMake(0, 0, 320, 44);
      mySearchBar.delegate = self;
      [mySearchBar sizeToFit];
      [mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
      
      [self.view addSubview:mySearchBar];  
      

      还有tableView

      UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, 320, 436)];
      [self.view addSubview:tableView]; 
      

      如果你想在xib中添加那么

      【讨论】:

        猜你喜欢
        • 2014-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-15
        • 2016-07-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多