Reputation: 2364
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 :
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.
Upvotes: 0
Views: 1604
Reputation: 349
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!
Upvotes: 7
Reputation: 2364
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.
Upvotes: 0
Reputation: 931
Check these settings, if there not
self.searchDisplayController.searchResultsDelegate = self; self.searchDisplayController.searchResultsDataSource = self;
try this,
Upvotes: 0