1

사용자가 수행 한 검색에서 UITableView의 결과를 표시 할 때 아래 오류가 발생합니다. 아래 코드는 검색 및 필터링이며 결과를 표시해야합니다. (하지만 그렇지 않습니다)사용자 검색에서 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]' 문자가 입력 되었으나 두 번째 문자와 충돌 함)

index.row마다 NSArray가 올바르게 작동하지 않는다는 대략적인 생각이 있습니다. 그러나 나는 이걸 어떻게 넘어야할지 모르겠다.

searchResults에서 발생한 일을보고하기 위해 @try - catch 문을 사용했습니다. 그것은이

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 
) 

어떤 도움이 좋을 것 생산했다.

+0

PFObject selectedObject * = [자기 objectAtIndexPath : indexPath]; [selectedObject objectForKey : @ "Name"];으로 차례로 사용됩니다. Parse.com에서 백엔드 데이터베이스의 개별 키 객체를 가져옵니다. – Evilelement

답변

0

시행 착오를 통해 해결 된 문제입니다.

이 방법도 없어진

  • (NSInteger)를 tableView (jQuery과 *)를 tableView numberOfRowsInSection (NSInteger) 섹션 { //가 정상 테이블이나 검색 결과 테이블에 표시되어 있는지 여부를 확인하고 해당 배열에서 카운트를 반환하십시오. if (tableView == self.searchDisplayController.searchResultsTableView) { return [searchResults count]; } else { return [bakeryProductArray count]; }}
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; 
+0

환호성 환호. 배열에는 "Tea Loaf", Tiramusu, "Treacle Loaf", Trifle, "Triple Chocolate Brownie", Truffles, "Various sponges", "Viennese Whirls", "Wedding Cakes"와 같은 데이터 문자열이 포함되어 있습니다. 데이터 소스 메소드는 첫 번째 문자를 입력 할 때 정확한 행 수를 표시하고, 두 번째 문자 만 입력하면 충돌합니다. – Evilelement

+0

추가 답변으로 내 답변을 편집했습니다. – Mundi

+0

시도해보십시오.나는 아직도 멍청하다. P – Evilelement

관련 문제