Ho implementato un UISearchDisplayController in UITableViewController. La ricerca funziona, ma UISearhDisplayController crea una nuova tabellaView con una cella standard e my TableView utilizza una cella personalizzata. Un altro problema è che io uso un segue:UISearchDisplayController e cella personalizzata
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
e le cellule in searchResultsTableView non attivano il Segue ..
Come per visualizzare i risultati di ricerca con cellule costume e Segue lavorare?
Questo è il codice:
...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView){
return [nameFilteredObj count];
} else {
return [sortedObj count];
}}
- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [[sortedObj objectAtIndex:indexPath.row] valueForKey:@"Name"];
} else {
cell.nameLabel.text = [[sortedObj objectAtIndex:indexPath.row] valueForKey:@"Name"];
cell.countryLabel.text = [[sortedObj objectAtIndex:indexPath.row] valueForKey:@"Country"];
cell.bottleImageView.image = [UIImage imageNamed:[[sortedObj objectAtIndex:indexPath.row] valueForKey:@"Image"]];
}
return cell;
}
//The codes for filtering the results and update of searchResultsTableView (not necessary for this question I think):
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[nameFilteredObj removeAllObjects];
for(NSDictionary *obj in sortedObj)
{
NSString *objName = [obj objectForKey:@"Name"];
NSRange range = [objName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[nameFilteredObj addObject:obj];
}}}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return NO;}
...
sto usando Storyboard come interfaccia.
è stata impostata su 'self.searchDislpayController.searchResultsDataSource' a' self'? E il 'searchResultsDelegate'? – rmaddy
Posso mostrarmi per favore? – ingenspor
'self.searchDisplayController.searchResultsDataSource = self'. Fatelo dove create il controller di ricerca. – rmaddy