过滤后的数组返回空,我不知道为什么?

时间:2013-06-06 02:46:09

标签: objective-c uitableview uiview nsarray

我正在使用PickerView过滤现有数组(称为“Strains”),并调用过滤后的数组“searchResults”。不知道为什么,但由于某种原因我的应用程序说searchResults是空的(即使Strains成功填充)。 crash error我是这样的:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array

任何想法为什么?代码如下。 注意:'Strains'正在从MySQL数据库中提取。

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


    }

    - (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];
        }


    }



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

    }

      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:@"Ailment CONTAINS[c] %@", [array1 objectAtIndex:[pickerView selectedRowInComponent:0]]];

        NSPredicate *actionPredicate = [NSPredicate predicateWithFormat:@"Action CONTAINS[c] %@", [array2 objectAtIndex:[pickerView selectedRowInComponent:1]]];

        NSPredicate *ingestPredicate = [NSPredicate predicateWithFormat:@"Ingestion CONTAINS[c] %@", [array3 objectAtIndex:[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 copy]];

                NSLog(@"%@", searchResults);
    }
}

TableViewController.m

- (int)numberOfSectionsInTableView: (UITableView *)tableview

{

    return 1;

}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == PickerTableView) {
        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 == strainTableView) {
           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);

2 个答案:

答案 0 :(得分:0)

此代码:

else
{
    return [array2 objectAtIndex:row];
}

永远不应该运行,但如果因为计数使用array1而可能会中断,那么您可以使用array2

对于您的谓词,selectedRowInComponent:返回一个整数,但您尝试使用它就像返回一个对象一样。我猜你想要更像的东西:

    NSPredicate *ailmentPredicate = [NSPredicate predicateWithFormat:@"Ailment CONTAINS[c] %@", [array1 objectAtIndex:[pickerView selectedRowInComponent:0]]];

    ...

(假设您的Strain个对象有一个名为Ailment的属性,并且您在array1中始终拥有> 0个对象。

在崩溃的任何地方,您应该在尝试使用之前检查阵列的count

答案 1 :(得分:0)

    [PickerTableView setSearchResults: searchResults];

可能会造成麻烦。当下一个场景加载时,它会被解除分配

制作searchResults的副本并使用它

    [PickerTableView setSearchResults: [searchResults copy]];

注意:如果在数组中使用customObjects

,请实现copyWithZone: