开发者

UISearchDisplayController with UITableViewController

开发者 https://www.devze.com 2023-03-18 06:37 出处:网络
I have a UITableViewController, in which I added a UISearchBar as the tableHeaderView using Interface Builder. Then I added a UISearchDisplayController in the nib, and set up all the connections (dele

I have a UITableViewController, in which I added a UISearchBar as the tableHeaderView using Interface Builder. Then I added a UISearchDisplayController in the nib, and set up all the connections (delegate, searchResultsDelegate, searchContentsController, searchResultsDataSource, all connected to the UITableViewController). I then implemented all the delegate and data source methods in my code.

It works like a charm, except for a weird bug: sometimes the search results table view won't scroll, and I can see the flash indicator of the main table view behind it. I NSLog'd the searchResultsTableView and apparently it's a subview of the main tableview, and I guess that's the reason behind the touch problems I described earlier.

What's my mistake? Is it possible to use a UITableViewController with UISearchDisplayController at all? If so, how do I set up it in such a way that the results table view doesn't get added as a subview of my main table view?

Update: I found this sample which uses UISearchDisplayController with UITableViewController and apparently the search table view gets added to the main table view in there as well. So now I don't think that's my problem.

The thing is that I can't find any substantial difference between what I'm doing and what that sample is doing. I'm just adding an UISearchBar as a UITableView header in a UITableViewController and adding a UISearchDisplayController to it... It's like iOS is confused between the main table and the search table when I try to scroll. Do you have any ideas?

Update: Added a 200 rep bounty. Please answer only if you know what y开发者_如何学运维ou're talking about.


It was an extra self.tableView.scrollEnabled = YES which was called as soon as the search data request was finished. Hope it helps anyone with the same problem in the future.


A search display controller manages display of a search bar and a table view that displays the results of a search of data managed by another view controller.

Here the searchDisplaycontroller combines the searchbar and tabelview which showing the result data in the tableview so it won't required separate tableview.

While when you using UISerarchBar then it need to have atleast one UITableView, which can show results in the view. And here you can see the result without even adding the SearchDisplayController to view.


I think you might want to try adding a UISearchDisplayController IBOutlet to your class and connect that to your SearchdDisplayController in the nib file. I am amusing that that searchdisplay controller in your nib automatically connected it self to the default serachdisaplaycontreoller outlet in the inspector.


The search bar controller uses its own table view that is added on top of your own. If you set the delegate and dataSource of the UISearchDisplayController to your UITableViewController, then the table view controller has to make a distinction between the two table views in its delegate methods. If you don't do that properly, this may be causing your bug.

In other words:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return number of rows in data model;
    } else {
        perform the search query;
        return number of rows in search query;
    }
}

You do something similar for all of the delegate and dataSource methods.

I'm not 100% sure that this is what's causing your problem, but without seeing more source it's hard to say.

0

精彩评论

暂无评论...
验证码 换一张
取 消