- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直无法在我的 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/
我需要在 didSelectRowAtIndexPath 方法内部再次调用 UITableView 中的 didSelectRowAtIndexPath 方法。 代码如下: -(void)tableV
我正在尝试在单元格创建 cellForRowAt 中手动调用 didSelectRowAtIndexPath,因为这将更新详细 View 而无需实际触摸该行,但是当我这样做时所以我在实际委托(dele
出于某种原因,我无法在以下代码中的第一个 IF 语句之后访问任何变量。例如,如果索引路径为[0,0],则变量phoneText 会输出电话号码。但如果它是 [1,0] 或 [2,0],我会得到“nul
我知道之前提到过这个问题,但那里的解决方案并不适用。我有一个使用 IB 设置的带有嵌入式 UITableViewController 的 UINavigationController 。在IB中,UI
我遇到了 ios 5 的 UITableView 的 didSelectRowAtIndexPath 问题,这在 ios 4 中是正确的。 第一次点击表中的任何行时,不会调用该方法。一旦我选择另一行,
我有一个 Viewcontroller,它是 UITableViewController 的子类。基本上我的问题是当我们点击主按钮时委托(delegate)方法被调用: - (void)tableVi
我在 uitextfield 正下方有一个 uitableview,它在 uitableview 单元格 中填充类似的字符串。项目被填充。但是,长按后会调用 didSelectRowAtIndexPa
所以我试图默认在我的 View 首次加载时选择 tableView 中的第一行。我正在这样做我的 viewWillAppear 方法: NSIndexPath *tempPath= [NSIndexP
我想在某个条件不满足时中止这个方法,我该怎么做? 我不使用 tableView:willSelectRowAtIndexPath: 方法。我认为可以结合这两种方法来防止某些行被选择并被推送到另一个 V
我有一个分为2个部分的UItableView。 我如何管理tableView didSelectRowAtIndexPath方法? 对于第1部分,该方法正在运行,但是对于第二部分,我不知道如何实现。
在我的应用执行期间的各个时间点,都需要使用以下命令以编程方式选择UITableView的一行 [self.tableView selectRowAtIndexPath:selectedCellInde
我有一个自定义的 UITableViewCell,其中包含一些 TextView 和一个 ImageView 。这些 View 是不可选择的(在 IB 中设置)。我希望当用户点击单元格内的任意位置时触
我用它创建了一个表和一个搜索栏 Controller 。我使用了一个结构来创建表内容。我只想将所选单元格中的数据存储在变量中。我是一名新的 iOS 开发人员。请帮忙:) TIA //my struct
我在表格 View 中有 5 个自定义单元格,但 didSelectRowAtIndexPath 仅适用于一个单元格。我已在代码中该单元格的索引处添加了该行的单元格。 let cell : FileS
我现在正在弄乱 table ,我的目标是最终能够点击 table 并转到某个地方。但是,我无法使 didSelectRowAtIndexPath 工作。该表显示正常,但当我选择它时没有任何反应: cl
didSelectRowAtIndexPath:即使在将表委托(delegate)和数据源连接到文件所有者后,方法也不会调用。 当我试图打印一些东西时,它没有用这种方法打印 在下面找到我的代码供您引用
我在 viewController 中有两个 UITableView。两者都有不同的标签。带有标签 0 的 UITableview 是可编辑的,而标签 1 是不可编辑的。我的问题是当在带有标签 1 的
我正在尝试学习在我的应用程序中使用核心数据。我怎么会被困在这里 didSelectRowAtIndexPath 如果可能的话,我希望能够像以前一样使用我的 uitableview arrayA 是一个
我有一个 UITableView,其中填充了 30 多个 UITableviewCell。 我面临的问题是,当选择一行并更改单元格的 accessoryType 时,该行的第十一行也会被修改。 我已经
在我的 UITableView 中,我有 2 行。我正在尝试禁用第二行,因此我完成了图片中显示的以下操作。当我运行该程序时,虽然该行无法推送到另一个 View ,但当我单击它时蓝色突出显示仍然闪烁。
我是一名优秀的程序员,十分优秀!