单击UISearchController中的CANCEL导航到以前的ViewController

时间:2015-12-05 03:35:49

标签: objective-c uisearchcontroller

我在ViewDidLoad

中创建了UISearchController
//create the search controller
    self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchResultsUpdater = self;
    self.searchController.delegate = self;
    self.definesPresentationContext = YES;
    [self.searchBarHolder addSubview: self.searchController.searchBar];
    self.searchController.searchBar.delegate = self;

当用户点击CANCEL按钮时,将调用以下委托方法。

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    self.searchController.active = NO;
    self.myTableView.hidden = YES;
    self.transparentViewSearch.hidden = YES;
}

self.searchController.active=NO行将用户导航回上一个视图控制器。

此外,self.transparentViewSearch.hidden = YES行无效。视图仍然可见。

发生了什么事?

1 个答案:

答案 0 :(得分:0)

you are using same table for showing search result
 self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];

取消搜索结果后,您需要更新表格。

[self.tableview reloadData];

然后你需要做类似下面的事情。

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(bShowSearchResult)
    {
        // return search result count
    }
    else
    {
        // return normal list count
    }
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(bShowSearchResult)
    {
         // create cell using search result data
         Data = [lstSearchResults objectAtIndex:indexPath.row];
         //create cell
         // return cell
    }
    else
    {
         Data = [lstFiles objectAtIndex:indexPath.row];
         //create cell
         // return cell
    }
}

我认为这会对你有所帮助