Reputation: 1017
I am trying to implement a search bar with my table view. Both my table view and search bar are placed using storyboard. In my ViewDidLoad, I have:
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
and implemented two search methods:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"nickname contains[cd] %@",searchText];
NSArray* array = [[NSArray alloc]init];
array = [self.fbfriendList filteredArrayUsingPredicate:bPredicate];
self.searchResult = [NSMutableArray arrayWithArray:array];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
In my table view method:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"addFriend";
cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FFAddFriendTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
NSLog(@"!!! %@",self.searchResult);
if (cell == nil) {
[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"FFAddFriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"addFriend"];
}
else{
NSLog(@"!!! %@",self.searchResult);
// LINE XXXXXXX
cell.nameLabel.text = [[self.searchResult objectAtIndex:indexPath.row] objectForKey:@"nickname"];
}
}
else
{
}
cell.nameLabel.text = [[self.fbfriendList objectAtIndex:indexPath.row] objectForKey:@"nickname"];
return cell;
}
Everything works accordingly in terms of search. My NSLogs prints out the correct data in my array. However, when I want to set the cell's label according to the data from the NSMutableArray, it crashes with the error:
-[__NSCFString setText:]: unrecognized selector sent to instance 0x7ff38b5b70e0
Am I missing any crucial fundamental to implementing the search bar to my tableView?
Upvotes: 0
Views: 35
Reputation: 1084
now change your table view cellrowindex Method to this..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"addFriend";
cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FFAddFriendTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.nameLabel.text = [[self.searchResult objectAtIndex:indexPath.row] objectForKey:@"nickname"];
} else
{
cell.nameLabel.text = [[self.fbfriendList objectAtIndex:indexPath.row] objectForKey:@"nickname"];
}
return cell;
}
Upvotes: 1
Reputation: 104082
In the line below "// LINE XXXXXX", you have,
cell.nameLabel = ...
That should be,
cell.nameLabel.text = ...
Upvotes: 2