UISearchDisplayController - how to preload searchResultTableView

前端 未结 8 1234
深忆病人
深忆病人 2020-12-08 10:56

I want to show some default content when the user taps the Searchbar, but before any text is entered.

I have a solution working using settext:

- (voi         


        
8条回答
  •  孤街浪徒
    2020-12-08 11:06

    OK, I have it. Preload your dataSource with some data and do the following hack (nothing illegal) and you'll get the tableView to show up. Note that there may be some clean-up to do, but this will get your default data to display.

    In viewDidLoad of the view controller than owns the UISearchBar:

    [super viewDidLoad];
    [self.searchDisplayController.searchResultsTableView reloadData];
    // whatever you named your search bar - mine is property named searchBar
    CGRect testFrame = CGRectMake(0, 20, self.searchBar.frame.size.width, 100);
    self.searchDisplayController.searchResultsTableView.frame = testFrame;
    [self.searchBar.superview addSubview:self.searchDisplayController.searchResultsTableView];
    

    Here's what's happening:
    The UISearchBar doesn't want to show the searchResultsTableView until you start editing. If you touch the tableView (e.g. reloadData) it will load and be there, sitting in memory with frame = CGRectZero. You give it a proper frame and then add it to the superview of the searchBar, et voila.

    I verified this is correct by displaying the superview of the searchBar and the superview of the tableView after a proper load of the tableView - they have the same superview. So, I go back and add the tableView to the superview early, and sure enough it shows up. For me, it showed up dimmed (maybe another view above it? alpha set lower?), but was still clickable. I didn't go any further, but that definitely gets you your tableView displaying without any user interaction.

    Enjoy,

    Damien

提交回复
热议问题