当搜索栏处于活动状态时,indexPath行在didSelectRowAtIndexPath方法中始终返回0

时间:2014-03-31 02:58:03

标签: ios uitableview uisearchbar

我在我的iPhone应用程序中使用搜索栏时遇到了麻烦,这是一个饮料词典。它可以很好地加载核心数据加载所有内容,并且在搜索时可以完美地过滤,但是当我完成搜索后选择饮料时,它总是会返回列表中的第一项。我应该提一下,我正在根据饮料类型进行fetchedResultsController和普通提取请求的混合(在这个例子中,我在执行大数据拉取时使用了获取的结果控制器,并且使用了用户创建的数据的正常提取请求)。这是我的相关代码:

- (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];
}

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

//TODO handle drink selection
NSManagedObjectContext *context = [self managedObjectContext];
 Drink *theDrink;
NSLog(@"index path %d", indexPath.row);
NSLog(@"search index %d", self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.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:^{

}];

}

这最后一种方法令人不安。不在搜索中时,它可以正常工作,但由于某种原因,只要搜索处于活动状态,它就无法识别索引。谢谢你的帮助。

编辑:添加行计数代码

- (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];
    }
}

}

1 个答案:

答案 0 :(得分:1)

解决了这个问题。问题是我正在使用" self.tableView"初始化SWTableViewCell。而不是" tableView"正在被传入。因此UISearchTableView没有被传入选择搞乱一切。