我一直无法在我的 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/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我正在尝试将以下SQL查询转换为ActiveRecord,它正在融化我的大脑。deletefromtablewhereid有什么想法吗?我想做的是限制表中的行数。所以,我想删除少于最近10个条目的所有内容。编辑:通过结合以下几个答案找到了解决方案。Temperature.where('id这给我留下了最新的10个条目。 最佳答案 从您的SQL来看,您似乎想要从表中删除前10条记录。我相信到目前为止的大多数答案都会如此。这里有两个额外的选择:基于MurifoX的版本:Table.where(:id=>Table.order(:id).