سؤال

I have a UIViewController class that contains a UITableView and a SearchDisplayController. The problem I'm facing is that whenever I click on the search bar and type a query.. it displays both the search results AND displays the underlying UITableView for some reason.

so for example if I run this code while i'm running search:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        ..
        NSLog(@"constructing a search result table view cell ");            
    } else
    {
        ..
        NSLog(@"::: constructing a mail box  table view cell");
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSLog(@"searchDisplayController: number of rows: %d", [queryResultData count]);
    } else {
        NSLog(@"mailbox: NUMBER OF ROWS: %d", [self.emailData count]);
    }
}

the log will look like this:

[5033:c07] performing search with query hi
[5033:c07] ftSearch started with query = hi dbNum = 0
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] searchDisplayController: number of rows are 1
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 2
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 3
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 4
[5033:c07] ::: constructing a search result table view cell 
[5033:c07] searchDisplayController: number of rows are 5
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] searchDisplayController: number of rows are 6
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] mailbox: NUMBER OF ROWS IN SECTION: 19
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell
[5033:c07] ::: constructing a mail box  table view cell

this is what the IB look like:

enter image description here

and

enter image description here

To be clear.. the displayed UI is exactly as I want it.. but I'm just confused as to why the other table is doing all these rendering calculations.. this later becomes a real problem b/c it's giving me some threading/asynchronous code headache and it's really hard to debug while this background and unnecessary activity is taking place.

note:I've also tried different layouts with the IB.. one thing I noticed was that if I made the search bar an adjacent subview to the tableView.. the underlying table will be called even more!

I've also thought of creating a flag and checking if the search bar is currently in use, and then basically prevent the underlying UITAbleView from doing any work.. but that's not good enough.. it shouldn't be called in the first place.

Can someone tell me how to prevent the underlying table from appearing/being rendered at all when i'm using search?

هل كانت مفيدة؟

المحلول

this doesn't directly address my concern.. but it limits the damage somewhat. I created a local variable that stores if the search is active:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    isSearchViewDisplayed = YES;
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    isSearchViewDisplayed = NO;
}

I noticed upon further inspection that the first thing the tableview asks its UITableViewDataSource is the number of sections.. everything (ie number of rows per section, and rendering of cells) happens after that.. so I simply returned 0 at the section checking part:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.searchDisplayController.searchResultsTableView 
           || !isSearchViewDisplayed) {
        return 1;
    } else {
        return 0;
    }
}

I won't mark this as correct.. just waiting to see if there is a better answer out there.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top