在UITableView上显示UISearchController向下滚动

在UITableView上显示UISearchController向下滚动

问题描述:

我有一个UISearchController刚过我的UITableView,我的桌子上有很多recrods。当用户进入桌子底部时,他必须回到顶部才能看到搜索栏。下面你可以看到我是如何创建的搜索栏:在UITableView上显示UISearchController向下滚动

- (void) showSearchBar { 
    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 

    self.searchController.searchResultsUpdater = self; 
    self.searchController.searchBar.placeholder = nil; 
    [self.searchController.searchBar sizeToFit]; 
    self.tableView.tableHeaderView = self.searchController.searchBar; 
    //self.sharedNavigationItem.titleView = _searchController.searchBar; 

    self.searchController.delegate = self; 
    self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES 
    self.searchController.searchBar.delegate = self; // so we can monitor text changes + others 
     self.definesPresentationContext = YES; 
    _searchController.hidesNavigationBarDuringPresentation = NO; 

} 

我不知道如何来显示搜索栏时,我在表中向下滚动(上)和隐藏它回来时,我向上滚动(下去)在桌上我甚至在桌子底部。

搜索栏可以随意放置在UI中。创建表格视图的同级视图,例如放置在表格上方。给这种观点的高度约束初始设置为44px,并提供出口到两个视图和约束......

@property(weak,nonatomic) IBOutlet UIView *searchBarContainerView; 
@property(weak,nonatomic) IBOutlet NSLayoutConstraint *searchBarHeightConstraint; 

现在,您的设置更改代码:

// ... 
self.searchBarHeightConstraint.constant = self.searchController.searchBar.bounds.size.height; 
[self.searchBarContainerView addSubview:self.searchController.searchBar]; 
// ... 

如果使头部留在上面,然后在搜索字段中应保持可见

UITableViewStylePlain:一个普通的表视图。任何节头或页脚都显示为内嵌分隔符,并在滚动表格视图时浮动。

UITableViewStyleGrouped:一个表视图,其各节显示不同的行组。部分页眉和页脚不会浮动。

+0

这是好消息。所以我猜OP可以将样式设置为plain,并将搜索栏放置在0头部中?这很好理解,但应该指出,这种方法会限制其他表行为(例如,OP希望在节标题中另外添加其他内容,或者不希望其他标题浮动等) – danh

+0

他可以定义它只适用于所选表格,这并不需要适用于所有表格 –

我想帮你与我所做的一切,希望它可以帮助你。 这是我如何在表视图的标题视图中添加我的标签,我试图用您的搜索控制器替换它;

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
     CGRect frame = CGRectMake(0, 0, tableView.frame.size.width, 40); 
     UIView *view = [[UIView alloc] initWithFrame:frame]; 
     view.backgroundColor = [UIColor clearColor]; 
     view.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
     _searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
     self.searchController.searchResultsUpdater = self; 
     self.searchController.searchBar.placeholder = nil; 
     [self.searchController.searchBar sizeToFit]; 
     [view addSubview:_searchController]; 
     view.tag = 123; 
     return view; 
} 

如果您希望隐藏拖动结束拖动动作时使用下面的方法;

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView 
        withVelocity:(CGPoint)velocity 
      targetContentOffset:(inout CGPoint *)targetContentOffset{ 
    if (velocity.y > 0){ 
    NSLog(@"up"); 
    [self.view viewWithTag:123].hidden = true; 
} 
if (velocity.y < 0){ 
    NSLog(@"down"); 
    [self.view viewWithTag:123].hidden = false; 
} 

而对于下面的滚动方法的tableview使用的即时变化:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView //working delegate 
{ 
    if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) { 
     NSLog(@"down"); 
     [self.view viewWithTag:123].hidden = false; 
    } else { 
     NSLog(@"up"); 
     [self.view viewWithTag:123].hidden = true; 
    } 
} 

一些搜索我想一些新的答案后,希望它可以帮助你!

要确定方向滚动的:

BOOL showSearch; 

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) { 
     // down 
     showSearch = true; 
    } else { 
     // up 
     showSearch = false; 
    } 
} 

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView 
        withVelocity:(CGPoint)velocity 
      targetContentOffset:(inout CGPoint *)targetContentOffset{ 
    if (showSearch) { 
     [self addSearch]; 
    } 
    else { 
     [self removeSearch]; 
    } 
    [_tableView reloadData]; 
} 

要添加&删除SearchController:

-(void)addSearch {  
    if (!_searchController){ 

    _searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 

    _searchController.searchResultsUpdater = self; 
    [_searchController.searchBar sizeToFit]; 
    _tableView.tableHeaderView = _searchController.searchBar; 

    _searchController.delegate = self; 
    _searchController.dimsBackgroundDuringPresentation = NO; 

    self.definesPresentationContext = YES; 
    _searchController.active = NO; 

    _searchController.searchResultsUpdater = self; 
    _searchController.dimsBackgroundDuringPresentation = NO; 
    self.definesPresentationContext = YES; 
    _tableView.tableHeaderView = _searchController.searchBar; 

    _searchController.searchBar.delegate = self; 
    } 
} 

-(void)removeSearch { 
    _searchController.active = NO; 
    [_searchController removeFromParentViewController]; 
    _searchController = nil; 
    _tableView.tableHeaderView = nil; 
}