0

因此,我开发了一个应用程序,该应用程序当前具有填充的 UITableView。UITableView 当前填充了来自 MySQL 数据库的数据。UITableView 还包含一个工作搜索栏。

然而,要添加到这一点,我们现在想要添加一个 3 列 UIPickerView 到组合中(已经编码)。

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

一切都是编码的,我只是不确定如何将两者联系起来。我该怎么做呢?到目前为止,我已将我们的代码粘贴在下面(对于冗长的帖子表示歉意,但在这种情况下似乎都是有效的)。任何帮助深表感谢。

ViewController.h(表视图控制器)

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

    IBOutlet UITableView *strainTableView;

NSArray *Strains;
NSArray *searchResults;

NSMutableData *data;

}

@property (nonatomic, retain) NSArray *searchResults;



@end

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


        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
4

1 回答 1

0

您可以使用复合谓词过滤多个值

NSPredicate *TitlePredicate = [NSPredicate predicateWithFormat:@"Title contains[cd] %@", tittletex];

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

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

NSCompoundPredicate *resultPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: TitlePredicate,descriptionPredicate,ratingpredicate];

searchResults = [菌株过滤数组UsingPredicate:resultPredicate];

于 2013-05-22T06:40:48.677 回答