我正在尝试使用在 uipickerview (PickerViewController.m) 中选择的结果来过滤现有的 uitableview (TableViewController.m)。出于某种原因,当我单击应该过滤 tableview 的按钮(位于 PickerViewController 上的“GO”)时,它只会给我应该过滤的相同完整列表。我 NSLogged 我的表视图(使用搜索结果和使用完整列表),实际上,它没有返回我的过滤结果。有谁知道我该如何解决这个问题,并让它返回过滤后的结果?代码如下。任何关于如何调整此代码的建议将不胜感激:) 我觉得我应该在我的 TableViewController中添加某种if语句?
编辑:所以我更新了我的代码,看起来它正在正确地尝试过滤我的 Strains 数组。但是它现在告诉我 searchResults 是一个空数组?为什么是这样?为了测试我的 searchResults 是否正常工作,我只实现了一个简单的if语句。见下文。
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) NSArray *searchResults;
@property (nonatomic, retain) IBOutlet UIPickerView *pickerView;
- (void)populateArray1;
- (void)populateArray2;
- (void)populateArray3;
@end
@protocol PickerViewControllerDelegate <NSObject>
- (void)PickerViewControllerDidFinish:(PickerViewController*)viewController;
@end
PickerViewController.m
- (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"];
}
- (IBAction)buttonpressed:(UIButton *)sender {
NSLog(@"Button Pushed!");
}
- (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"])
{
NSLog(@"%@", Strains);
// Get reference to the destination view controller
PickerResultsTableViewController *PickerTableView = [(UINavigationController *)[segue destinationViewController] topViewController];
NSLog(@"%@", PickerTableView);
NSPredicate *ailmentPredicate = [NSPredicate predicateWithFormat:@"SELF['Ailment'] CONTAINS[c] %@", [pickerView selectedRowInComponent:0]];
NSPredicate *actionPredicate = [NSPredicate predicateWithFormat:@"SELF['Action'] CONTAINS[c] %@", [pickerView selectedRowInComponent:1]];
NSPredicate *ingestPredicate = [NSPredicate predicateWithFormat:@"SELF['Ingestion'] CONTAINS[c] %@", [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...
[PickerTableView setSearchResults: searchResults];
NSLog(@"%@", searchResults);
}
}
@end
PickerResultsTableViewController.h
#import "PickerViewController.h"
@interface PickerResultsTableViewController : UITableViewController {
IBOutlet UITableView *PickerTableView;
NSArray *Strains;
NSArray *searchResults;
NSMutableData *data;
}
@property (nonatomic, retain) NSArray *searchResults;
@end
PickerResultsTableViewController.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 == PickerTableView) {
NSLog(@"Using the search results");
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 = [[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:@"Title contains[cd] %@",
searchText];
searchResults = [Strains filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self 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];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)PickerViewControllerDidFinish:(PickerViewController *)viewController {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 82;
}