gpt4 book ai didi

ios - 当搜索栏处于事件状态时,indexPath 行在 didSelectRowAtIndexPath 方法中始终返回 0

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:18 26 4
gpt4 key购买 nike

我一直无法在我的 iPhone 应用程序(饮料词典)中使用搜索栏。它可以很好地加载核心数据,并且在搜索时可以完美过滤,但是当我完成搜索后选择饮料时,它总是返回列表中的第一项。我应该提一下,我正在根据饮料类型混合使用 fetchedResultsController 和普通获取请求(在这种情况下,我在执行大数据拉取和用户创建数据的正常获取请求时使用了获取结果 Controller )。这是我的相关代码:

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

最佳答案

为任何偶然发现此问题的人解决了这个问题。问题是我用“self.tableView”而不是传入的“tableView”初始化 SWTableViewCell。因此 UISearchTableView 没有在选择时传入,把一切都搞砸了。

关于ios - 当搜索栏处于事件状态时,indexPath 行在 didSelectRowAtIndexPath 方法中始终返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com