简体   繁体   中英

ios/objective-c: Search Display Controller combined with custom cell

I am using a search bar and search display controller that utilizes an NSPrediate to search a tableview created from core data. The following code works fine for most of my VCs. However, for one where I use a custom cell, whenever I search, I get zero results.

Here is the code in cellforrowatindexpath that adjusts datasource for the search:

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

Since this is the only case where the VC uses a custom cell, I'm wondering if that is why no results return.

This is line that tells cellforrowatindexpath to use a custom cell:

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

Finally, this is how the values in the cell are set:

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

Could the custom cell be responsible for no items appearing in search or should I look elsewhere for error?

Note: the count of the items returned upon search is always zero.

(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 {
}}

and in in number of row in section put

 (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.isSearch == YES) {
    return [self.arySearchClientList count];        
} else {
    return [aryClientList count];        
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM