0

NSPrediate を利用してコア データから作成されたテーブルビューを検索する検索バーと検索ディスプレイ コントローラーを使用しています。次のコードは、ほとんどの VC で正常に機能します。ただし、カスタム セルを使用している場合は、検索するたびに結果がゼロになります。

検索用にデータソースを調整する cellforrowatindexpath のコードは次のとおりです。

 Items *item;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        item = [searchResults objectAtIndex:indexPath.row];
    } else {
        item
        = [self.fetchedResultsController objectAtIndexPath:indexPath];
    }

これは VC がカスタム セルを使用する唯一のケースであるため、結果が返されないのはそのためではないかと考えています。

cellforrowatindexpathこれは、カスタム セルを使用するよう指示する行です。

customCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.item = item;

最後に、セル内の値が設定される方法は次のとおりです。

-(void) setItem:(Items *)item{ //open 1
    _item = item;
    self.nameLabel.text = item.name;
}

検索結果にアイテムが表示されないのは、カスタム セルのせいでしょうか?

注: 検索時に返されるアイテムの数は常にゼロです。

4

1 に答える 1

0
(IBAction)onbtnSearchTapped:(id)sender {
@try{
    self.strSearchName = self.txtSearchName.text;


    if ([self.strSearchName isEqualToString:@""] &&
        [self.strSearchVehicleType isEqualToString:@""] ) {
        self.isSearch = NO;
    }
    else {
        self.isSearch = YES;
        NSString* filter = @"%K CONTAINS[cd] %@ || %K CONTAINS[cd] %@";

        NSMutableArray *args=[[NSMutableArray alloc]initWithObjects:@"sFirstName",self.strSearchName,nil];

        NSArray *arrSearch=[NSArray arrayWithArray:args];
        self.arySearchList =[NSMutableArray arrayWithArray:[aryList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:filter argumentArray:arrSearch]]];

        DLog(@"%@",self.arySearchList);
        self.strSearchName = @"";

    }
    [self.tableView reloadData];
} @catch (NSException *exception) {
    NSLog(@"Exception At: %s %d %s %s %@", __FILE__, __LINE__, __PRETTY_FUNCTION__, __FUNCTION__,exception);
}
@finally {
}}

およびセクションの行数で

 (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.isSearch == YES) {
    return [self.arySearchClientList count];        
} else {
    return [aryClientList count];        
}}
于 2016-01-08T05:56:46.620 に答える