2017-04-22 1 views
0

Je souhaite effectuer une recherche dans mon UITableView en conservant les images à côté du texte. Je ne peux pas comprendre le problème, je dis que le crash se produit dès que vous tapez du texte.UISearchBar avec NSArray (la recherche dans UITableView ne fonctionne pas)

Voici mon code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    if (isSearching) { 
     return [filteredContentList count]; 
    } 
    else { 
     return [contentList count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 
    if (isSearching) { 
     NSDictionary *searchResult = [filteredContentList objectAtIndex:indexPath.row]; 

     NSString *uppercaseString = [[searchResult objectForKey:@"title"] uppercaseString]; 
     cell.nameLabel.text = uppercaseString; 

     NSString *stringImage = [searchResult valueForKeyPath:@"thumbnail.file"]; 

     [cell.parallaxImage sd_setImageWithURL:[NSURL URLWithString:stringImage] 
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

     [cell.parallaxImage setShowActivityIndicatorView:YES]; 
     [cell.parallaxImage setIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    } 
    else { 
     NSDictionary *searchResult = [contentList objectAtIndex:indexPath.row]; 

     NSString *uppercaseString = [[searchResult objectForKey:@"title"] uppercaseString]; 
     cell.nameLabel.text = uppercaseString; 

     NSString *stringImage = [searchResult valueForKeyPath:@"thumbnail.file"]; 

     [cell.parallaxImage sd_setImageWithURL:[NSURL URLWithString:stringImage] 
           placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

     [cell.parallaxImage setShowActivityIndicatorView:YES]; 
     [cell.parallaxImage setIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    } 

    return cell; 

} 

- (void)searchTableList { 
    NSLog(@"title"); 

    NSString *searchText = searchBar.text; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title CONTAINS[cd] %@", searchText]; 

    filteredContentList = [contentList filteredArrayUsingPredicate:predicate]; 

    NSLog(@"filtered : %@", filteredContentList); 
} 

#pragma mark - Search Implementation 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
    isSearching = YES; 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    NSLog(@"Text change - %d",isSearching); 

    //Remove all objects first. 
    //[filteredContentList removeAllObjects]; 

    [self searchTableList]; 
    if([searchText length] != 0) { 
     isSearching = YES; 
     [self searchTableList]; 
    } 
    else { 
     isSearching = NO; 
    } 
    [self.tblContentList reloadData]; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
    NSLog(@"Cancel clicked"); 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    NSLog(@"Search Clicked"); 

    [self searchTableList]; 
} 

(dans mon code le NSPredicate sont entièrement fonctionnels, mais j'ai un accident :)

*** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UITableView.m:8174 
2017-04-22 10:24:59.538 TableSearch[13649:2676593] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UISearchResultsTableView: 0x7d444000; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x7de362c0>; layer = <CALayer: 0x7de35f50>; contentOffset: {0, -64}; contentSize: {375, 1936}>) failed to obtain a cell from its dataSource (<ViewController: 0x7dc37b00>)' 
*** First throw call stack: 
///////// 

///////// 
libc++abi.dylib: terminating with uncaught exception of type NSException 

enter image description here

+0

Pour autant que je puisse voir à partir du rapport d'erreur, le problème se situe dans '[UISearchResultsTableView _configureCellForDisplay: forIndexPath:]'. Avez-vous remplacé cette méthode? Si oui, pourriez-vous également spécifier le code? –

+0

@AleksandrMedvedev Tout mon code est allumé, quelque chose me manque? –

+1

1er - Une petite suggestion: puisque vous avez deux états, la recherche et non, et la configuration des cellules est identique, vous pouvez juste quelque chose comme: NSArray * datasource = isSearching? filteredContentList: contentList; Ensuite, utilisez la source de données pour configurer la cellule. Cela réduira la quantité de code que vous avez. 2e question: avez-vous appelé [tableView reloadData] lorsque vous passez de la recherche à la non-recherche? – genalipsis

Répondre

0

// Une meilleure utilisation deux rangées

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ 

    if (searchText.length == 0) { 
     isSeraching = false; 
      [_tableView reloadData]; 
    }else{ 

     isSeraching = true; 

NSString *searchString = searchBar.text; 
      NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString]; 
      [filteredArray addObjects:[searchArray filteredArrayUsingPredicate:predicate]]; 
    [_tableView reloadData]; 

    } 


} 

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

    if (isSeraching) { 
     return searchArray.count; 
    }else{ 
     return filteredArray.count; 
    } 
} 


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

    if (isSeraching) { 

     SearchNewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell"]; 
     cell.searchingLabel.text = [searchArray objectAtIndex:indexPath.row]; 
     return cell; 

    }else{ 

    RTSearchRecentViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recentSearch" forIndexPath:indexPath]; 
    cell.recentTextLabel.text = [filteredArray objectAtIndex:indexPath.row]; 
    return cell; 
    } 
} 
0

Votre problème est cette méthode:

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

comme il peut retourner nil.

Vous devez utiliser dequeueReusableCellWithIdentifier:forIndexPath:, qui assure une cellule instancié dans tous les cas:

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

Cette exception vous dit que tableView:cellForRowAtIndexPath: retourné nil, mais il ne faut pas.