出于某种原因,NSPredicate 没有过滤我的 UITableView?
Posted
技术标签:
【中文标题】出于某种原因,NSPredicate 没有过滤我的 UITableView?【英文标题】:For some reason, NSPredicate is not filtering my UITableView? 【发布时间】:2013-05-26 16:19:27 【问题描述】:由于某种原因,NSPredicate 没有过滤我的 UITableView(它应该通过 UIPickerView 中的选择过滤我的 TableView)。用户做出他们的 pickerview 选择,按下 GO 按钮(segue 从 Pickerview 附加到 Table View 控制器)。
知道为什么它不起作用吗?请参阅下面的代码。
ViewController.m(表视图控制器)
- (int)numberOfSectionsInTableView: (UITableView *)tableview
return 1;
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if (tableView == self.searchDisplayController.searchResultsTableView)
return [searchResults count];
else
return [Strains count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *strainTableIdentifier = @"StrainTableCell";
StrainTableCell *cell = (StrainTableCell *)[tableView dequeueReusableCellWithIdentifier:strainTableIdentifier];
if (cell == nil)
cell = [[StrainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strainTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StrainTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
if (tableView == self.searchDisplayController.searchResultsTableView)
NSLog(@"Using the search results");
cell.titleLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Description"];
cell.ratingLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Rating"];
cell.ailmentLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Ailment"];
cell.actionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Action"];
cell.ingestLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Ingestion"];
NSLog(@"%@", searchResults);
else
NSLog(@"Using the FULL LIST!!");
cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
cell.descriptionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Description"];
cell.ratingLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Rating"];
cell.ailmentLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Ailment"];
cell.actionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Action"];
cell.ingestLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Ingestion"];
return cell;
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [Strains filteredArrayUsingPredicate:resultPredicate];
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
[se
lf filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
StrainDetailViewController *detailViewController = [[StrainDetailViewController alloc] initWithNibName:@"StrainDetailViewController" bundle:nil]; if ([searchResults count])
detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
detailViewController.strainDetail = [searchResults objectAtIndex:indexPath.row];
else
detailViewController.title = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
detailViewController.strainDetail = [Strains objectAtIndex:indexPath.row];
NSLog(@"%@", Strains);
[self.navigationController pushViewController:detailViewController animated:YES];
- (void)PickerViewControllerDidFinish:(PickerViewController *)viewController
[self.navigationController popViewControllerAnimated:YES];
PickerViewController.h
@protocol PickerViewControllerDelegate;
@interface PickerViewController : UIViewController
UIPickerView *pickerView;
NSMutableArray *array1;
NSMutableArray *array2;
NSMutableArray *array3;
NSArray *Strains;
NSArray *searchResults;
NSMutableData *data;
- (IBAction)buttonpressed:(UIButton *)sender;
@property (nonatomic, weak) id<PickerViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIPickerView *pickerView;
- (void)populateArray1;
- (void)populateArray2;
- (void)populateArray3;
@end
@protocol PickerViewControllerDelegate <NSObject>
- (void)PickerViewControllerDidFinish:(PickerViewController*)viewController;
@end
PickerViewController.m
@implementation PickerViewController
- (void)handleCloseButton:(id)sender
if ([self.delegate respondsToSelector:@selector(PickerViewControllerDidFinish:)])
[self.delegate PickerViewControllerDidFinish:self];
@synthesize pickerView;
#pragma mark -
#pragma mark picker view methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
return 3;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
if (component == 0)
NSLog(@"you selected %@", [array1 objectAtIndex:row]);
if (component == 1)
NSLog(@"you selected %@", [array2 objectAtIndex:row]);
if (component == 2)
NSLog(@"you selected %@", [array3 objectAtIndex:row]);
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
if (component == 0)
return [array1 count];
if (component == 1)
return [array2 count];
if (component == 2)
return [array3 count];
else
return [array1 count];
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
if (component == 0)
return [array1 objectAtIndex:row];
if (component == 1)
return [array2 objectAtIndex:row];
if (component == 2)
return [array3 objectAtIndex:row];
else
return [array2 objectAtIndex:row];
- (void)populateArray1
array1 = [[NSMutableArray alloc] init];
[array1 addObject:@"Arthritis"];
[array1 addObject:@"Cancer"];
[array1 addObject:@"HIV"];
[array1 addObject:@"Migraines"];
[array1 addObject:@"Insomnia"];
- (void)populateArray2
array2 = [[NSMutableArray alloc] init];
[array2 addObject:@"Nausea"];
[array2 addObject:@"Pain"];
[array2 addObject:@"Appetite"];
[array2 addObject:@"Fever"];
[array2 addObject:@"Exhaustion"];
- (void)populateArray3
array3 = [[NSMutableArray alloc] init];
[array3 addObject:@"Oil"];
[array3 addObject:@"Plant"];
[array3 addObject:@"Edible"];
[array3 addObject:@"Powder"];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"pickerGo"])
// Get reference to the destination view controller
ViewController *strainTableView = [(UINavigationController *)[segue destinationViewController] topViewController];
NSPredicate *ailmentPredicate = [NSPredicate predicateWithFormat:@"Ailment contains[cd] %@", [pickerView selectedRowInComponent:0]];
NSPredicate *actionPredicate = [NSPredicate predicateWithFormat:@"Action contains[cd] %@", [pickerView selectedRowInComponent:1]];
NSPredicate *ingestPredicate = [NSPredicate predicateWithFormat:@"Ingestion contains[cd] %@", [pickerView selectedRowInComponent:2]];
NSCompoundPredicate *resultPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: ailmentPredicate,actionPredicate,ingestPredicate, nil]];
searchResults = [Strains filteredArrayUsingPredicate:resultPredicate];
// Pass any objects to the view controller here, like...
[strainTableView setSearchResults: searchResults];
【问题讨论】:
我猜你的过滤器数组过滤后会变空,只需检查过滤方法中searchResults
的值,希望你能解决问题,祝你好运
searchResults 最初用于附加到我的 uitableview 的搜索栏。我应该为过滤的 Picker 结果更改“searchResults”吗?我完全被这个问题搞糊涂了;大声笑。
是的,您可以在相同的UITableview
中显示结果,只需管理标志以识别要显示的数据
【参考方案1】:
你的谓词:
NSPredicate *resultPredicate =
[NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
searchResults = [Strains filteredArrayUsingPredicate:resultPredicate];
看起来正在尝试使用“应变”对象(您尚未给出定义,但肯定是复合对象)作为字符串对象。
CONTAINS[cd] 是一个字符串比较函数。
定义您的谓词更像是您用于选择器的复合版本。
【讨论】:
我很困惑?我以为应变是在 ViewController.m 中定义的? “定义谓词更像复合版本”是什么意思? 你有一个 NSCompoundPredicate *resultPredicate 被选择器使用。这是进行过滤的正确方法,因为它使用对象的内容。对于表格视图,您尝试使用单个谓词,但这是行不通的。 哦,我明白你在说什么!好的,所以我将 tableview 上的谓词更改为如下所示: - (void)filterContentForPicker:(NSString*)searchText NSCompoundPredicate *resultPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: ailmentPredicate,actionPredicate,ingestPredicate, nil]] ; searchResults = [菌株过滤数组UsingPredicate:resultPredicate];但它是说 ailmentPredicate 和 actionPredicate 是未定义的?我在这里错过了一些超级基本的东西吗?哈哈。谢谢! 从外观上看,选择器有 3 个组件,那么单个“searchText”是从哪里来的。选择器谓词由每个组件的部分组成: NSPredicate *ailmentPredicate = [NSPredicate predicateWithFormat:...]; NSPredicate *actionPredicate = [NSPredicate predicateWithFormat:...]; NSPredicate *ingestPredicate = [NSPredicate predicateWithFormat:...]; NSCompoundPredicate *resultPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: ailmentPredicate,actionPredicate,ingestPredicate, nil]]; 好的,现在(例如),我有这个: NSPredicate *ingestPredicate = [NSPredicate predicateWithFormat:@"Ingestion contains[cd] %@", [UIPickerView selectedRowInComponent:2]]; NSCompoundPredicate *resultPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: ailmentPredicate,actionPredicate,ingestPredicate, nil]];仅将一个组件显示为 cmets 不会让我粘贴所有代码,哈哈。但它说'没有已知的选择器'SelectedRowinComponent'的类方法。以上是关于出于某种原因,NSPredicate 没有过滤我的 UITableView?的主要内容,如果未能解决你的问题,请参考以下文章
出于某种原因,我的 D3 地图显示颠倒了 - 我该如何翻转它?
PAX 出于某种原因杀死了我的进程。 (PAX 用 SIGKILL 终止我的进程)
React -- Material UI -- Popover, setAnchorEl(null) onClose 的 popover 出于某种原因没有将其设置为 null