Search code examples
iosobjective-cuitableviewios9uisearchbar

SearchController not searching custom UITableViewCells


Not sure if the title makes to much sense so here is a description of what is going on:

I have a UITableViewController that is using a custom UITableViewCell for its data. I am then manually adding a searchBar to the header of the UITableView and setting it up based on this tutorial : http://useyourloaf.com

Now the searchBar is setup, it looks like it is working but the issue is that I am not actually getting any results and the table is not loading properly (the search results, it load the base data fine)

Here is my code for comparison. I know I must be missing something simple...

** I am hardcoding the data for my cells at the moment, this will change to a core data model once I can fix this issue... although this may be the base of my issue as I am hardcoding the cells at each IP **

@implementation CharitiesTableViewController{
NSArray *charities;
NSArray *searchResults;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setColors];

    [_charityTable registerNib:[UINib nibWithNibName:@"CharityTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"charityCell"];

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 350.0;
//    self.tableView.contentInset = UIEdgeInsetsMake(-2.0f, 0.0f, 0.0f, 0.0);

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.searchController.delegate = self;

    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void) setColors {

}

#pragma mark - Search Controller

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = searchController.searchBar.text;
    [self searchForText:searchString];
    [self.tableView reloadData];
}

- (void)searchForText:(NSString *)searchText {
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    searchResults = [charities filteredArrayUsingPredicate:resultPredicate];
}

- (void)willPresentSearchController:(UISearchController *)searchController {
    self.navigationController.navigationBar.translucent = YES;
}

-(void)willDismissSearchController:(UISearchController *)searchController {
    self.navigationController.navigationBar.translucent = NO;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == _charityTable)
    {
        return 1;
    }
    return [searchResults count];
}

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

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
    return SectionSpacer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return SectionSpacer;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:{
            CharityTableViewCell *cell = (CharityTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"charityCell"];

            cell.charityImage.image = [UIImage imageNamed:@"cleanup"];
            cell.charityName.text = @"Garbage cleanup - Crowchild";
            cell.charityTagLine.text = @"City of Calgary";
            cell.charityDescriptionShort.text = @"We are rounding up anyone that wants to help clean up the grass and nearby areas close to crowchild.";

            return cell;
        }
        default:{
            UITableViewCell *cell;
            return cell;
        }
    }
}

@end

Thanks for you help!


Solution

  • I am silly.... I was trying to do this without initializing the data like a com mentor said. The code is correct and if anyone has any similar issues make sure your array of data actually has data in it...