我的搜索工作非常正常,通常在过滤时,但在我选择一个单元后,didSelectRowAtIndex indexPath参数的indexPath始终为null,并且传递给该方法的tableView始终是UITableView而不是UISearchTableView,这意味着常用的if(tableView == self.searchDisplayController.searchResultsTableView)语句总是返回false,即使if(self.searchDisplayController.isActive)将正确返回true。感谢您的任何帮助或建议。
以下是相关代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([self.currentDrinkType.name isEqualToString:@"Beer"] || [self.currentDrinkType.name isEqualToString:@"Wine"] || [self.currentDrinkType.name isEqualToString:@"Liquor"] ){
id sectionInfo =
[[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}else{
if (tableView == self.searchDisplayController.searchResultsTableView){
return [_searchResults count];
}else{
return [_drinks count];
}
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"drink";
SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Drink *drinkCell;
if ([self.currentDrinkType.name isEqualToString:@"Beer"] || [self.currentDrinkType.name isEqualToString:@"Wine"] || [self.currentDrinkType.name isEqualToString:@"Liquor"] ){
drinkCell = [_fetchedResultsController objectAtIndexPath:indexPath];
}else{
if (tableView == self.searchDisplayController.searchResultsTableView){
drinkCell = [_searchResults objectAtIndex:indexPath.row];
}else{
drinkCell = [_drinks objectAtIndex:indexPath.row];
}
}
//Check if cell object has already been favorited, if it has render unfavorite button instead
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
NSString *favoriteText;
if ([drinkCell.isFavorite boolValue]){
favoriteText = @"Unfavorite";
}else{
favoriteText = @"Favorite";
}
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]
title:favoriteText];
//Can only delete a drink if it is custom
if ([drinkCell.isCustom boolValue] && drinkCell.isCustom != nil){
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
title:@"Delete"];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.231f green:0.231f blue:1.0f alpha:1.0f]
title:@"Edit"];
}
cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier
containingTableView:self.tableView // For row height and selection
leftUtilityButtons:nil
rightUtilityButtons:rightUtilityButtons];
cell.delegate = self;
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath tableView:tableView];
return cell;}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView {
Drink *drinkCell;
if ([self.currentDrinkType.name isEqualToString:@"Beer"] || [self.currentDrinkType.name isEqualToString:@"Wine"] || [self.currentDrinkType.name isEqualToString:@"Liquor"] ){
drinkCell = [_fetchedResultsController objectAtIndexPath:indexPath];
}else{
if (tableView == self.searchDisplayController.searchResultsTableView){
drinkCell = [_searchResults objectAtIndex:indexPath.row];
}else{
drinkCell = [_drinks objectAtIndex:indexPath.row];
}
}
cell.textLabel.text = drinkCell.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%%", drinkCell.alcoholByVolume];
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
NSPredicate *resultPredicate;
if ([self.currentDrinkType.name isEqualToString:@"Beer"] || [self.currentDrinkType.name isEqualToString:@"Wine"] || [self.currentDrinkType.name isEqualToString:@"Liquor"]){
resultPredicate= [NSPredicate predicateWithFormat:@"SELF.name contains[cd] %@ AND drinkType ==%@",searchText, self.currentDrinkType];
[NSFetchedResultsController deleteCacheWithName:[NSString stringWithFormat:@"Root%@", self.currentDrinkType]];
[_fetchedResultsController.fetchRequest setPredicate:resultPredicate];
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}else{
resultPredicate= [NSPredicate predicateWithFormat:@"SELF.name contains[cd] %@",searchText];
_searchResults = [[_drinks filteredArrayUsingPredicate:resultPredicate]mutableCopy];
}
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObjectContext *context = [self managedObjectContext];
Drink *theDrink;
if (tableView == self.searchDisplayController.searchResultsTableView){
NSLog(@"search index %@", [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]);
}
selectedIndexPath = indexPath;
NSLog(@"selected index path %d", selectedIndexPath.row);
NSManagedObject *selectedDrink;
if ([self.currentDrinkType.name isEqualToString:@"Beer"] || [self.currentDrinkType.name isEqualToString:@"Wine"] || [self.currentDrinkType.name isEqualToString:@"Liquor"] ){
theDrink = [_fetchedResultsController objectAtIndexPath:indexPath];
selectedDrink = [_fetchedResultsController objectAtIndexPath:indexPath];
}else{
if (self.searchDisplayController.isActive){
selectedDrink = [_searchResults objectAtIndex:indexPath.row];
theDrink = [_searchResults objectAtIndex:indexPath.row];
}else{
selectedDrink = [_drinks objectAtIndex:indexPath.row];
theDrink = [_drinks objectAtIndex:indexPath.row];
}
}
[selectedDrink setValue:[NSDate date] forKey:@"lastChosen"];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
self.selectedDrink = theDrink;
[self.delegateDrink drinkTableViewControllerDidFinish:self];
[self dismissViewControllerAnimated:YES completion:^{
}];
}
答案 0 :(得分:0)
以UITableView实例参数开头的委托方法需要区分常规表视图和搜索结果视图。索引路径参数与表视图匹配(即,常规或搜索)。检查您是否获得了适合您期望的表视图的索引路径。