【问题标题】:Search Results not visible搜索结果不可见
【发布时间】:2013-09-07 18:46:54
【问题描述】:

我有一个带有搜索显示控制器的表格视图。当我键入搜索时,日志显示搜索正在正常工作并且过滤正常,但屏幕仅显示“无结果”。如果这有所作为,我将使用 Core Data。

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

【问题讨论】:

  • 你在哪里更新单元格的内容?
  • 就在下面:Instance *instance = [self.fetchedResultsController objectAtIndexPath:indexPath]; //更多搜索数据 if (tableView == self.searchDisplayController.searchResultsTableView) { cell.textLabel.text = [searchResults objectAtIndex:indexPath.row]; } else { cell.textLabel.text = instance.name; }
  • @Isaac:您可以将该代码添加到您的问题中吗?这使得它更容易阅读。 - numberOfSectionsnumberOfRowsInSection 方法也会很有帮助。
  • 您是否查看过显示“无结果”消息的逻辑并从那里返回以了解为什么它认为没有结果?
  • (格式会很好。)

标签: iphone ios uitableview core-data uisearchdisplaycontroller


【解决方案1】:

检查numberOfSectionsInTableViewnumberOfRowsInSection 的实现。
首先,如果是搜索表视图(1 部分),则应返回 1。
numberOfRowsInSection 中,您还应该像在numberOfSectionsInTableView 中一样检查给定的tableView 参数并返回[searchResults count]

【讨论】:

    【解决方案2】:

    你的实现不应该是这样的:-

    - (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 而不是数组。

    【讨论】:

    • 感谢您。我实现了它并插入了日志语句以查看代码是如何运行的。一切都被看到了,在日志中我可以看到搜索准确地过滤了我的搜索,但显示仍然显示没有结果。我应该看看其他地方吗?
    • 如果您看到一条物理消息“No Results”,那么是否有任何代码可以输出此消息?
    • 我不这么认为。在 CellForRowAtIndexPath 下,如果 tableView == self.searchResultsController.searchResultsTableView,我定义了应该填充单元格的内容。那不应该配置搜索结果单元格吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2018-12-08
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2021-01-25
    相关资源
    最近更新 更多