搜索结果不可见

时间:2013-09-07 18:46:54

标签: iphone ios uitableview core-data uisearchdisplaycontroller

我有一个带有搜索显示控制器的表格视图。当我键入搜索时,日志显示搜索正在运行并正确过滤,但屏幕仅显示“无结果”。我正在使用核心数据,如果这有所不同。

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [searchResults count];
}
else{
    return[[self.fetchedResultsController sections]count];
}
}



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

id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
return [secInfo numberOfObjects];

}

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

if (tableView != self.tableView) {
    NSLog(@"Found searchDisplayController");
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} else {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSLog(@"Found regular table View");
}

    // Configure the cell...
    Instance *instance = [self.fetchedResultsController objectAtIndexPath:indexPath];


    if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
    } else {
    cell.textLabel.text = instance.name;
    }

这是NumberOfRowsInSection和Number:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
     if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [searchResults count];
}
     else{
    return[[self.fetchedResultsController sections]count];
}
}
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
     return [secInfo numberOfObjects];
 }

2 个答案:

答案 0 :(得分:0)

检查numberOfSectionsInTableViewnumberOfRowsInSection的实施情况 在第一个中,如果是搜索表视图(1节),则应返回1 在numberOfRowsInSection中,您还应该像在numberOfSectionsInTableView中一样检查给定的tableView参数并返回[searchResults count]

答案 1 :(得分:0)

您的实施不应该是这样的: -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
     if (tableView == self.searchDisplayController.searchResultsTableView) 
     {
       return 1;
     }
     else
     {
       return[[self.fetchedResultsController sections]count];
     }
}
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
    if(tableView == self.searchDisplayController.searchResultsTableView)
    {
      return [searchResults count];
    }
     id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
     return [secInfo numberOfObjects];
 }

如果搜索结果的数量很高,那么我建议使用另一个Fetched Results Controller而不是数组。

相关问题