2016-01-08 29 views
0

我正在使用搜索栏和搜索显示控制器,它利用NSPrediate来搜索从核心数据创建的tableview。以下代码适用于我的大多数VC。但是,对于使用自定义单元格的情况,只要搜索,我就会得到零结果。ios/objective-c:结合自定义单元格的搜索显示控制器

这里是的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; 
} 

能自定单元负责没有项目出现在搜索中,或者我应该在别处寻找错误?

注意:搜索时返回的项目总数为零。

回答

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];   
}} 
相关问题