1

尝试通过用户执行的搜索在 UITableView 中显示结果时生成以下错误。下面的代码正在搜索、过滤并应该显示结果。(但不是)

@implementation ProductsViewController

{
    NSPredicate *searchSearch;
    NSArray *results;
}

@synthesize bakeryTableView, bakeryProductArray, searchResults, itemName;

-(void)viewDidLoad
{
    bakeryProductArray = [[NSMutableArray alloc]init];
    searchResults = [[NSArray alloc]init];
    [self testermethod];
    [[self navigationController] setNavigationBarHidden:NO];
    [super viewDidLoad];
}

-(void)testermethod
{
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    // Retrieve the top songs by sales
    [query orderByAscending:@"Name"];

    // Limit to retrieving ten songs
    query.limit = 100;
    [query findObjectsInBackgroundWithBlock:^(NSArray *food, NSError *error) {
        if (error) return;

        // Songs now contains the last ten songs, and the band field has
        // been populated. For example:
        for (PFObject *song in food) {

            PFObject *simple = [song objectForKey:@"Name"];

            [bakeryProductArray addObject: simple];
        }
        [bakeryTableView reloadData];
        return;
        NSLog(@"Passed");
    }];
}


-(id)initWithCoder:(NSCoder *)aCoder {
    self = [super initWithCoder:aCoder];
    if (self) {
        self.parseClassName = @"JackiesBakeryProducts";
        self.textKey = @"objectid";
        self.pullToRefreshEnabled = YES;
        self.paginationEnabled = NO;
       // self.objectsPerPage = 20;
    }

    return self;
}

-(PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];

    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.
    if ([self.objects count] == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    [query orderByAscending:@"Name"];

    return query;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //Fill table in with data
    static NSString *cellIdentifier = @"Cell";
    PFObject *selectedObject = [self objectAtIndexPath:indexPath];
    NSLog(@"%@", selectedObject);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {

            NSString *selectedObject = [searchResults objectAtIndex:indexPath.row];
            NSLog(@"%i", searchResults.count);
            cell.textLabel.text = selectedObject;
        }

    else {

        NSString *productName = [selectedObject objectForKey:@"Name"];
        NSString *productQuantity = [selectedObject objectForKey:@"Quantity"];

        cell.textLabel.text = productName;
        cell.detailTextLabel.text = productQuantity;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

#pragma mark - UITableView Delegate methods

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Deal with user search from tableview
    if (bakeryTableView == self.searchDisplayController.searchResultsTableView)
    {

        [self performSegueWithIdentifier: @"bakeryDetails" sender:self];

    }

    //Prep the next view controller with data from current view (data passing)
    detailedViewController *productinfo = [self.storyboard instantiateViewControllerWithIdentifier:@"bakeryDetails"];

    PFObject *selectedObject = [self objectAtIndexPath:indexPath];

    productinfo.productName = [selectedObject objectForKey:@"Name"];
    productinfo.productImage = [selectedObject objectForKey:@"ImageUrl"];
    productinfo.productDescription = [selectedObject objectForKey:@"Description"];
    productinfo.productPrice = [selectedObject objectForKey:@"Price"];
    productinfo.productQuantity = [selectedObject objectForKey:@"Quantity"];
    productinfo.productAllergy = [selectedObject objectForKey:@"Allergy"];

    [self.navigationController pushViewController:productinfo animated:YES];
    [bakeryTableView deselectRowAtIndexPath:indexPath animated:YES];

}


//Becareful here as wrong predicateWithFormat can crash app on keying in search
-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope
{

    searchSearch = [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@",searchText];
    searchResults = [bakeryProductArray filteredArrayUsingPredicate:searchSearch];

    NSLog(@"Filtered Food Product Count --> %d",[searchResults count]);
    NSLog(@"%@", searchResults);
}


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];
    return YES;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Check to see whether the normal table or search results table is being displayed and return the count from the appropriate array
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [searchResults count];
    }
    else
    {
        return [bakeryProductArray count];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

有了上面的代码。由于未捕获的异常'NSRangeException',它正在生成一个错误*终止应用程序,原因:'**终止应用程序,原因:'* - [__NSArrayI objectAtIndex:]:索引1超出范围[0 .. 0]'(它确实使用输入的第一个字符进行搜索但与第二个崩溃)

我有一个粗略的想法,即 NSArray 在按 index.row 调用时无法正常工作。但除了我不知道如何克服这个。

使用 @try - catch 语句来报告 searchResults 中发生的情况。它产生了这个

2013-09-08 13:27:20.876 jackies[995:c07] Filtered Food Product Count --> 1
2013-09-08 13:27:20.876 jackies[995:c07] Aero
2013-09-08 13:27:20.877 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:20.877 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:20.877 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:20.877 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:20.877 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:20.877 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:20.878 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:20.878 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:32.550 jackies[995:c07] Filtered Food Product Count --> 1
2013-09-08 13:27:32.551 jackies[995:c07] Aero
2013-09-08 13:27:32.551 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:32.551 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:32.551 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:32.552 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:32.552 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:32.552 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:32.552 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:32.552 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:34.188 jackies[995:c07] Filtered Food Product Count --> 1
2013-09-08 13:27:34.189 jackies[995:c07] Aero
2013-09-08 13:27:34.189 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:34.189 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:34.189 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:34.189 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:34.189 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:34.190 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:34.190 jackies[995:c07] Caught this (
    Aero
)
2013-09-08 13:27:34.190 jackies[995:c07] Caught this (
    Aero
)

任何帮助都会很棒。

4

2 回答 2

1

如果您的数组包含字典,则谓词是错误的。如果他bakeryProductArray使用字符串数组而不是字典,它会起作用。否则你将不得不使用

@"name CONTAINS[cd] %@"

作为格式字符串。

还要确保根据搜索结果数组重新加载表 - 即检查您的数据源方法以返回正确的行数。

第二点:在区分两个表之前检索要显示的对象似乎不合逻辑。逻辑应该如下:

UITableViewCell *cell = // dequeue cell
if (tableView == self.searchDisplayController.searchResultsTableView) {
  // retrieve object at IndexPath.row
  // configure the cell to display it
}
else { 
  // do the same for main table view
}
return cell;
于 2013-09-07T22:33:32.433 回答
0

通过反复试验解决了问题。

也缺少方法

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 检查是否正在显示普通表或搜索结果表,并从适当的数组返回计数 if (tableView == self.searchDisplayController. searchResultsTableView) { return [searchResults count]; } else { 返回 [bakeryProductArray 计数]; } }
于 2013-09-08T16:27:46.940 回答