开发者

UISearchDisplayController - How to implement and get it working

I have spent hours reading a couple dozen different blogs and others q's here on SO about this, but I'm at an impasse.

My question is how do you make this thing work. I've tried it in IB and through code.

When I tried it in IB the search bar would never show even though I dragged it onto the table view (of a UITableViewController) and it "snapped" onto the table where the header belongs. But the search bar would never show up. (That's a sub-question, anyone know why?)

So I tried setting up just in code. I get it allocated, assign the delegate, and data source, but I never get that table view that says "No results". The bar showed up and I can type into it, but it always just shows an empty table even though I know there should be results. I am suspicious its not showing the right table since the cell w/ the "No results" text doesn't show.

 - (void)viewDidLoad {

    [super viewDidLoad];

    names = [[NSMutableArray alloc] initWithObjects:@"Michael", @"Dwight", @"Jim", @"Andy", @"Ryan", @"Creed", @"Darryl", @"Kevin", @"Oscar", @"Gabe", nil];
    results = [[NSMutableArray alloc] init];

    UISearchBar *sb = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    sb.tintColor = [UIColor greenColor];
    searchTable.tableHeaderView = sb;

    UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self];
    [self setSearchDisplayController:sdc];
    [sdc setDelegate:self];
    [sdc setSearchResultsDataSource:self];}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.searchTable) {
        // normal table view population
        return [names count];
    }
    if(tableView == self.searchDisplayController.searchResultsTableView){
        // search view population
        return [results count];
    }
    return 0;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }

    if (tableView == self.searchTable) {
        // normal table view population
        cell.textLabel.text = [names objectAtIndex:indexPath.row];
    }
    if(tableView == self.searchDisplayController.searchResultsTableView){
        // search view population
    }

    cell.textLabel.textColor = [UIColor blueColor];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    if (ta开发者_StackOverflow社区bleView == self.searchTable) {
        // normal table view population
    }
    if(tableView == self.searchDisplayController.searchResultsTableView){
        // search view population
    }   
}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
    [results removeAllObjects]; // First clear the filtered array.

    /*
     Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
     */
    for (NSString *str in names)
    {
        NSComparisonResult result = [str compare:controller.searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [controller.searchBar.text length])];
        if (result == NSOrderedSame)
        {
            [results addObject:str];
        }
    }
}

Any help would be so very much appreciated. I would add a bounty, but as you can see, I've got "Newb" written on my forehead still :).


A couple possible points of failure I see:

1) are you setting your viewcontroller as a delegate in its .h file? example:

@interface myViewController : UIViewController <UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDataSource,UITableViewDelegate>{

2) I can't tell if that is your complete code, or just relevant snippets, but if it's the full code there are a couple holes before you will see any data showing up. You'd need to add a tableview somewhere in there to display the results into and fill out the second if statement in your cellForRowAtIndexPath method.

I'd recommend checking out the sample code for TableSearch from Apple, that ought to get you moving in the right direction.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜