0

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

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

知道为什么吗?代码如下。注意:“菌株”是从 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);
    }
}

表视图控制器.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);
4

2 回答 2

0

这段代码:

else
{
    return [array2 objectAtIndex:row];
}

永远不应该运行,但如果它可能会因为计数使用array1而中断,并且在这里你使用array2.

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

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

    ...

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

无论崩溃发生在哪里,您都应该count在尝试使用它之前检查数组。

于 2013-06-06T06:38:33.833 回答
0
    [PickerTableView setSearchResults: searchResults];

可能会导致问题。加载下一个场景时它会被释放

复制 searchResults 并使用它

    [PickerTableView setSearchResults: [searchResults copy]];

注意:copyWithZone:如果您在数组中使用自定义对象,请实施

于 2013-06-06T06:43:02.793 回答