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"
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 :
- 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.
- 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.