-->

UISearchDisplayController没有结果的tableView?(UISearchD

2019-06-27 10:00发布

通常,UISearchDisplayController,激活时,变暗的tableView和重点搜索栏。 只要您输入文本搜索栏很快,它会创建一个搜索栏和键盘之间显示searchResultsTableView。 加载后,这个第二的UITableView时searchDisplayController的委托被调用/显示/隐藏/卸载。 通常它显示实时搜索结果或在键入时自动完成条目。

在我的应用程序,我想搜索一个Web服务,我不想要求用户输入每个字母的web服务。 因此,我想完全禁用searchResultsTableView并保持灰色黑色覆盖,而他输入文本。 然后我会触发搜索(的加载屏幕),一旦他打的搜索按钮。

刚刚返回零行的searchResultsTableView并不好看,因为它显示了“没有结果”的消息一空searchResultsTableView。 我试图隐藏表时出现( searchDisplayController:didLoadSearchResultsTableView:它的工作原理,但涂黑暗淡覆盖还藏着使得下面的tableView是完全可见一次。

除了从头开始重新创建UISearchDisplayController功能的任何想法?

Answer 1:

这里是我刚刚想出了一个小动作,你也必须返回0的结果,同时编辑搜索字符串

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    savedSearchTerm = searchString;

    [controller.searchResultsTableView setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.8]];
    [controller.searchResultsTableView setRowHeight:800];
    [controller.searchResultsTableView setScrollEnabled:NO];
    return NO;
}

- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView
{
    // undo the changes above to prevent artefacts reported below by mclin
}

我想你会找出下一步该怎么做



Answer 2:

你有没有试过这样:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_lookup:) object:nil];
[self performSelector:@selector(_lookup:) withObject:txt afterDelay:0.20];

这样一来,如果用户类型中的1/5秒另一个字符,你只会让自己的网络电话。



Answer 3:

上面的似乎没有什么到底好工作,所以我想出了以下的(你要调用removeTableHeader当你准备显示您的结果):

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    [self setTableHeader];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self setTableHeader];
}

- (void)setTableHeader {
    UIView *headerView = [[UIView alloc] initWithFrame:self.searchDisplayController.searchResultsTableView.frame];
    headerView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];

    [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor clearColor]];
    [self.searchDisplayController.searchResultsTableView setScrollEnabled:NO];
    [self.searchDisplayController.searchResultsTableView setTableHeaderView:headerView];

    [headerView release];
}

- (void)removeTableHeader {
    [self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor whiteColor]];
    [self.searchDisplayController.searchResultsTableView setScrollEnabled:YES];
    [self.searchDisplayController.searchResultsTableView setTableHeaderView:nil];
}

显然,这使表透明的,添加具有相同大小的表黑色/半透明的表头,并禁用滚动在桌子上,所以你不能得到上述的或过去的头。 作为奖励,你可以添加一些标题视图(“请稍候...”或活动的指标)。



Answer 4:

有同样的问题,因为你,我处理得由)什么时候开始搜索,然后b)添加/移除OverlayView的到ViewController的看法的searchResultsTableView的alpha设置为0。 作品对我来说就像魅力。

@interface MyViewController()
//...
@property(nonatomic, retain) UIView *overlayView;
//...
@end

@implementation MyViewController
@synthesize overlayView = _overlayView;

//...

- (void)viewDidLoad
{
    //...

    //define your overlayView
    _overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 480)];
    _overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
}

//hide the searchResultsTableView
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    self.searchDisplayController.searchResultsTableView.alpha = 0.0f;
}

//when ending the search, hide the overlayView
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    [_overlayView removeFromSuperview];
}

//depending on what the user has inputed, add or remove the overlayView to the view of the current viewController 
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{   

    if ([searchString length]>0) 
    {
        [self.view addSubview:_overlayView];
    }
    else
    {
        [_overlayView removeFromSuperview];
    }

    return NO;
}

@end


Answer 5:

它应该足以实现你的UISearchDisplayDelegate下面的方法(这通常是自定义的UITableViewController子类)

- (BOOL) searchDisplayController: (UISearchDisplayController *) controller shouldReloadTableForSearchString: (NSString *) searchString
{
    [self startMyCustomWebserviceSearchAsBackgroundProcessForString: searchString]; //starts new NSThread
    return NO; 
}

你尝试过吗?



Answer 6:

基于user182820的代​​码下面是我的版本。 我隐藏UISearchDisplayController的表视图。 当在搜索框中输入一个字符,我把一个“灰色视图”,所以它看起来像UISearchDisplayController的“暗视”从来没有离开过,然后删除它时,搜索完成。 如果你输入一些字符,然后按取消,表视图短暂地进入全白,我不知道怎么去解决这个问题。

- (void)viewDidLoad {
    ...
    tableViewMask=[UIView new];
    tableViewMask.backgroundColor = [UIColor blackColor];
    tableViewMask.alpha = 0.8;
}

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
    tableViewMask.frame=CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y+controller.searchBar.frame.size.height, self.tableView.frame.size.width, self.tableView.frame.size.height-controller.searchBar.frame.size.height);
    controller.searchResultsTableView.hidden=YES;
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{
    [tableViewMask removeFromSuperview];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    if (searchString.length==0)
        [tableViewMask removeFromSuperview];
    else 
        [self.tableView addSubview:tableViewMask];
    [searchText autorelease];
    searchText=[searchString retain];
    return NO;
}   


Answer 7:

怎么样只是做这么简单:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
self.searchDisplayController.searchResultsTableView.hidden=YES;
return YES;
}

正常工作对我..



Answer 8:

我foud一个更好的办法,因为不存在与“最佳答案”的错误 - 分离器和“无结果”将滚动黑色背景表时显示。

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    controller.searchResultsTableView.backgroundColor = [UIColor blackColor];
    controller.searchResultsTableView.alpha = 0.8;
    controller.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    for(UIView *subview in tableView.subviews) {
        if([subview isKindOfClass:UILabel.class]) {
            subview.hidden = YES;
        }
    }

    return NO;
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {

    self.searchDisplayController.searchResultsTableView.backgroundColor = [UIColor whiteColor];
    self.searchDisplayController.searchResultsTableView.alpha = 1;
    self.searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    for(UIView *subview in tableView.subviews) {
        if([subview isKindOfClass:UILabel.class]) {
            subview.hidden = NO;
        }
    }

    // search results and reload data ....
}


Answer 9:

现有的所有答案都过于复杂。 你可以只隐藏立即将结果表视图脱身。

-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
    tableView.hidden = YES;
}


Answer 10:

我想我找到了这个问题的一个更好的实现。 以前所有的答案正确显示暗淡的看法相同什么的UITableView看起来像一个搜索之前,但每个解决方案缺乏挖掘区域以取消搜索功能。

出于这个原因,我认为这个代码工作得更好。

首先,创建一个BOOL如searchButtonTapped指示搜索按钮是否被窃听。 默认情况下,它是NO。

然后:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
if (!searchButtonTapped) {        
    // To prevent results from being shown and to show an identical look to how the tableview looks before a search
    [controller.searchResultsTableView setBackgroundColor:[UIColor clearColor]];
    [controller.searchResultsTableView setRowHeight:160];
    self.searchDisplayController.searchResultsTableView.scrollEnabled = NO;
} else {
    // Restore original settings
    [controller.searchResultsTableView setBackgroundColor:[UIColor whiteColor]];
    [controller.searchResultsTableView setRowHeight:44];
    self.searchDisplayController.searchResultsTableView.scrollEnabled = YES;
}

return YES;
}

这个现在应该基于他的答案是明确的。 确保当用户点击搜索按钮也恢复原来的设置。

此外,在cellForIndexPath方法添加:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.contentView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8];

为了创建输入文本之前所显示的一样黯淡看法。 请务必将这些属性右边的单元格,即检查该UITableView的是活动的,用户已不被窃听的搜索按钮。

然后,重要的是,在didSelectRowAtIndexPath方法:

if (tableView == self.searchDisplayController.searchResultsTableView) {
    if (searchButtonTapped) {
           // Code for when the user select a row after actually having performed a search
    {
else 
    [self.searchDisplayController setActive:NO animated:YES];

现在,用户可以点击暗淡的区域,这会不会导致一个UITableViewCell的可见选择,而是取消搜索。



文章来源: UISearchDisplayController with no results tableView?