我如何让我的应用程序搜索一个现有的UITableView的值在一个UIPickerView中选择



所以我开发了一个应用程序,目前有一个填充的UITableView。UITableView当前使用来自MySQL数据库的数据填充。UITableView也包含一个工作的搜索栏。

然而,为了增加这一点,我们现在想要添加一个3列的UIPickerView到混合(已经编码)。

思路:用户选择3个值(每列一个),然后点击"GO"按钮。一旦他们选择了他们的值并按下GO,我们希望应用程序通过uitableview过滤,只显示包含他们在PickerView中选择的值的结果。

一切都是编码的,我只是不确定如何将两者连接起来。我该怎么做呢?我将我们的代码粘贴到下面(很抱歉这篇文章很长,但在这种情况下似乎都是有效的)。如有任何帮助,不胜感激。

ViewController.h (TABLEVIEW CONTROLLER)

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
    IBOutlet UITableView *strainTableView;
NSArray *Strains;
NSArray *searchResults;
NSMutableData *data;
}
@property (nonatomic, retain) NSArray *searchResults;

@end

ViewController。m (tableviewcontroller)

- (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) {

        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"];

        NSLog(@"%@", searchResults);
    } else {
        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"];

    }
    {
    } 

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
{
    [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];
}

PickerViewController.h

@interface PickerViewController : UIViewController {

    UIPickerView *pickerView;
    NSMutableArray *array1;
    NSMutableArray *array2;
    NSMutableArray *array3;

    NSArray *Strains;
    NSArray *searchResults;
    NSMutableData *data;

    }
    - (IBAction)buttonpressed:(UIButton *)sender;
    @property (nonatomic, retain) IBOutlet UIPickerView *pickerView;

        - (void)populateArray1;
        - (void)populateArray2;
        - (void)populateArray3;
    @end

**PickerViewController.m**

    #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"];

    }

 - (IBAction)buttonpressed:(UIButton *)sender {
        NSLog(@"Button Pushed!");
    NSPredicate *TitlePredicate = [NSPredicate predicateWithFormat:@"Title contains[cd] %@", [pickerView selectedRowInComponent:0]];
    NSPredicate *descriptionPredicate = [NSPredicate predicateWithFormat:@"Description contains[cd] %@", [pickerView selectedRowInComponent:1]];
    NSPredicate *ratingpredicate = [NSPredicate predicateWithFormat:@"Rating contains[cd] %@", [pickerView selectedRowInComponent:2]];
    NSCompoundPredicate *resultPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: TitlePredicate,descriptionPredicate,ratingpredicate, nil]];
                                           filterResults = [Strains filteredArrayUsingPredicate:resultPredicate];

}

    @end

可以使用复合谓词对多个值进行过滤

NSPredicate *TitlePredicate = [NSPredicate predicateWithFormat:@"Title包含[cd] %@", titletex];

NSPredicate *descriptionPredicate = [NSPredicatepredicateWithFormat:@"Description contains[cd] %@", descriptiontext];

NSPredicate *ratingpredicate = [NSPredicatepredicateWithFormat:@"Rating contains[cd] %@", ratingtext];

nscompound谓词* result谓词= [nscompound谓词andPredicateWithSubpredicates:(NSArray arrayWithObjects:TitlePredicate、descriptionPredicate ratingpredicate];

searchResults = [strain filteredArrayUsingPredicate:resultPredicate];