【问题标题】:How can I make my app search an existing UITableView for values selected in a UIPickerView?如何让我的应用在现有 UITableView 中搜索 UIPickerView 中选择的值?
【发布时间】:2013-05-21 21:25:48
【问题描述】:

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

不过,除此之外,我们现在想要添加一个 3 列的 UIPickerView 到组合中(已经编码)。

想法:用户选择 3 个值(每列一个),然后点击“开始”按钮。一旦他们选择了他们的值并按下 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

【问题讨论】:

    标签: objective-c uitableview view xcode4.5 uipickerview


    【解决方案1】:

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

    NSPredicate *TitlePredicate = [NSPredicate predicateWithFormat:@"Title 包含[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 = [应变过滤ArrayUsingPredicate:resultPredicate];

    【讨论】:

    • 因此,与其让选择器搜索标题文本、描述文本和评分文本,我可以在这些地方放置什么代码以使其仅搜索/过滤选择器中选择的 3 个结果?谢谢:)
    • 我已经更新了原始帖子中的代码。看看我到目前为止用什么替换了它 [pickerView selectedRowInComponent:x]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多