Keaton Burleson
Keaton Burleson

Reputation: 508

UISearchController on iOS not calling cellForRowAtIndexPath

In my Xcode project, I have a TableView with elements, I added a search bar with Apple's 'amazing new' UISearchController. It was functioning fine for a bit, but now 'cellForRowAtIndexPath' isn't being called on the UISearchController. Results are passing, I NSLogg'ed the count, but the cell isn't being configured. Any ideas why this is happening?

TableViewController.m:

    - (void)viewDidLoad {
    [super viewDidLoad];



    self.listTableView.tableFooterView = [[UIView alloc] init];
     // Create new HomeModel object and assign it to _homeModel variable
    _displayModel = [[DisplayModel alloc]init];
    _displayModel.delegate = self;
    [_displayModel downloadItems];
    _listTableView.delegate = self;

    // Set this view controller object as the delegate for the home model object
    self.filteredArray = [NSMutableArray arrayWithCapacity:[self.athletes count]];

#define ENABLE_SCOPE_BUTTONS 0

    // The table view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
    UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"TableSearchResultsNavController"];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

    self.searchController.searchResultsUpdater = self;

    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);

    self.listTableView.tableHeaderView = self.searchController.searchBar;

#if ENABLE_SCOPE_BUTTONS

    NSMutableArray *scopeButtonTitles = [[NSMutableArray alloc] init];
    [scopeButtonTitles addObject:NSLocalizedString(@"All", @"Search display controller All button.")];

    for (NSString *sport in [Athlete sportTypeNames]) {
        NSString *displayName = [Product displayNameForType:deviceType];
        [scopeButtonTitles addObject:displayName];
    }

    self.searchController.searchBar.scopeButtonTitles = scopeButtonTitles;
    self.searchController.searchBar.delegate = self;

#endif

    //self.searchController.searchBar.scopeButtonTitles = scopeButtonTitles;

    self.searchController.searchBar.delegate = self;

      self.definesPresentationContext = YES;

;

    // Do any additional setup after loading the view, typically from a nib.
}

UISearchController.m

//
//  SearchResultsTableViewController.m
//  Sample-UISearchController
//
//  Created by James Dempsey on 9/20/14.
//  Copyright (c) 2014 Tapas Software. All rights reserved.
//

#import "SearchResultsTableViewController.h"
#import "DetailViewController.h"
#import "AthleteModel.h"

@implementation SearchResultsTableViewController

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.searchResults count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"SearchResultCell" forIndexPath:indexPath];
if (cell == nil) {
    Athlete *athlete = [self.searchResults objectAtIndex:indexPath.row];

    cell.textLabel.text = athlete.name;
    NSLog(@"CELL Called");
}
    return cell;


}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Athlete *athlete = [self.searchResults objectAtIndex:indexPath.row];
    DetailViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"DetailViewController"];
    self.presentingViewController.navigationItem.title = @"Search";
    vc.athlete = athlete;
    [self.presentingViewController.navigationController pushViewController:vc animated:YES];
}

-(void)viewDidLoad{
    Athlete *athlete = [self.searchResults objectAtIndex:0];
    self.tableView = self.tableView;
    self.tableView.frame = CGRectMake(0, 0, 320, 480);
    NSLog(@"Name: %@", athlete.name);
}

@end

Upvotes: 2

Views: 1009

Answers (5)

Steve Moser
Steve Moser

Reputation: 7807

Your issue is in theses lines:

// The table view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
    UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"TableSearchResultsNavController"];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

You are making the UINavigationController the search results controller not SearchResultsTableViewController. Thus cellForRowAtIndexPath is not being called on SearchResultsTableViewController. I'm surprised it isn't crashing. This should fix it:

// The table view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
    UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"TableSearchResultsNavController"];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:[searchResultsController.viewControllers firstObject]];

Upvotes: 0

Keaton Burleson
Keaton Burleson

Reputation: 508

Finally, I just switched back to a UISearchDisplayController based search. We're all happy now.

Upvotes: 0

codercat
codercat

Reputation: 23301

try this

searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ 
    if (searchController.searchResultsTableView == self.tableView)
    {
        return ...;
    }

    return ...;
}

referance link How do I use the UISearchBar and UISearchDisplayController

Upvotes: 0

Paulo
Paulo

Reputation: 1245

Assuming the delegate and datasource are setup - With or Without a Search Bar - The UITableview methods like CellforRowAtIndexPath execute one by one in sequence when triggered by this statement

     [self.myTable reloadData]; 

Just wondering If you have a Searchbar/ SearchCOntroller - where is the Search logic

         - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar  {
       // ....

       [self.myTable reloadData]; 
         }

Upvotes: 1

Nhut Duong
Nhut Duong

Reputation: 612

Did you set delegate and datasource for table view?

self.tableView.delegate = self;
self.tableView.dataSource = self;

or something like that.

Upvotes: 0

Related Questions