0
  • I have a UITableView containing custom cells. All works fine. But after, I decided to add a searchBar in order to... Search !

  • So, I added a "Search Bar and Search Display Controller" from the "Object Library" in my xib file, under the UITableView.

  • I created a specific class for the custom cell :

    "CustomCell.h"

    @class CustomCell;
    
    @interface CustomCell : UITableViewCell
    {
    }
    
    @property (weak, nonatomic) IBOutlet UILabel *lblDate;
    @property (weak, nonatomic) IBOutlet UILabel *lblAuteur;
    
    @end
    

    "CustomCell.m"

    no interesting stuff
    

    "CustomCell.xib"

    enter image description here

  • The "Event" class :

    @interface Event : NSObject
    {
    }
    
    @property (strong, nonatomic) NSString *desc;
    @property (strong, nonatomic) NSString *dateCreation;
    
    @end
    
  • And the view containing the UITableView and the UISearchBar :

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"cell";
    
        // Determinate the current event
        Event *currentEvent;
        if (tableView == self.searchDisplayController.searchResultsTableView)
            currentEvent = [filteredArray objectAtIndex:indexPath.row];
        else
            currentEvent = [notFilteredArray objectAtIndex:indexPath.row];
    
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if(cell == nil)
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    
        cell.lblAuteur.text = [currentEvenement desc];
        cell.lblDate.text = [currentEvenement dateCreation];
    
        return cell;
    }
    
  • Ok, now we can come to my problem. After loading the tableView, the custom cells are displaying well. But not if I use the SearchBar :

    1. If there is an event with the desc attribute equal to "foo", and if I enter "bar" in the SearchBar, I obtain the "No results" message. It's normal.
    2. If there is an event with the desc attribute equal to "foo", and if I enter "foo" in the SearchBar, the cells are displaying, but without their content ! I'm just seeing the cells' borders, and the lblDate and the lblAuteur are equal to nil.

Why have I this behaviour ? Thanks a lot for your help... I precise that the filteredArray and the notFilteredArray are correctly filled (I checked that many times). It means that the search mechanism is working well.

Jonathan F.
  • 2,357
  • 4
  • 26
  • 44

3 Answers3

7

After much "searching" (pun) I've found the problem.

When you reference the tableview with the prototype cell you can't rely on the tableview that is passed in because sometimes that tableview is the search tableview without the prototype cell.

so instead of...

HomeTabCell *cell = (HomeTabCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

replace the "tableview" above with a direct connection to the table view with the prototype cell

HomeTabCell *cell = (HomeTabCell *)[self.HomeTableView dequeueReusableCellWithIdentifier:CellIdentifier];

Hope this helps!

Marc Smith
  • 349
  • 4
  • 9
0

Check these settings, if there not

self.searchDisplayController.searchResultsDelegate = self; self.searchDisplayController.searchResultsDataSource = self;

try this,

Sandeep Ahuja
  • 931
  • 6
  • 17
  • I've done the links into Interface Builder between the **"Search Display Controller"** and the **"File's owner"**. Anyway, I tried to put these two lines into the viewDidLoad method, but the problem is still here... – Jonathan F. Dec 11 '13 at 14:33
0

Sorry for people searching to resolve this issue... I could not fix it, and the prime contractor do not want this function anymore, so I gave up.

Jonathan F.
  • 2,357
  • 4
  • 26
  • 44
  • It looks like I answered your question above, could you change the "selected answer" to help future searchers? – Marc Smith Jul 24 '19 at 17:39