2017-04-22 9 views
0

私は自分のUITableViewで画像をテキストの隣に置いて検索したいと思います。私は問題を理解することができない、私はあなたがテキストを入力するとすぐにクラッシュが起こると言います。ここでNSArrayを使用したUISearchBar(UITableViewでの検索は機能しません)

は私のコードです:

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

(私のコードでNSPredicateが完全に機能しているが、私はクラッシュ:)

*** 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

クラッシュレポートからわかるように、問題は '[UISearchResultsTableView _configureCellForDisplay:forIndexPath:]'にあります。このメソッドをオーバーライドしましたか?もしそうなら、そのコードも指定できますか? –

+0

@AleksandrMedvedev私のすべてのコードは、私は何かが恋しいですか? –

+1

1st - 1つの小さな提案:あなたは2つの状態を持つので、検索していないので、セルの設定は同じです。 NSArray * datasource = isSearching? filteredContentList:contentList; 次に、データソースを使用してセルを構成します。これにより、コードの量が削減されます。 2番目の質問:検索するかどうかを切り替えると[tableView reloadData]が呼び出されましたか? – genalipsis

答えて

0

//よりよい利用を持っています2つのアレイ

-(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; 
    } 
} 
01それは nilを返すことができるよう

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

0

あなたの問題は、この方法です。

あなたはすべてのケースでインスタンス化されたセルを保証する、dequeueReusableCellWithIdentifier:forIndexPath:を使用する必要があります。

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

その例外がtableView:cellForRowAtIndexPath:nilを返されたが、それはいけないことを語っています。

関連する問題