iOS 上的 UISearchController 不调用 cellForRowAtIndexPath

Posted

技术标签:

【中文标题】iOS 上的 UISearchController 不调用 cellForRowAtIndexPath【英文标题】:UISearchController on iOS not calling cellForRowAtIndexPath 【发布时间】:2015-01-12 01:54:35 【问题描述】:

在我的 Xcode 项目中,我有一个带有元素的 TableView,我添加了一个带有 Apple '惊人的新' UISearchController 的搜索栏。它运行得很好,但现在 UISearchController 上没有调用“cellForRowAtIndexPath”。结果通过了,我对计数进行了 NSLogg 操作,但未配置单元格。任何想法为什么会发生这种情况?

TableViewController.m:

    - (void)viewDidLoad 
    [super viewDidLoad];



    self.listTableView.tableFooterView = [[UIView alloc] init];
     // Create new HomeModel object and assign it to _homeModel variable
    _displayModel = [[DisplayModel alloc]init];
    _displayModel.delegate = self;
    [_displayModel downloadItems];
    _listTableView.delegate = self;

    // Set this view controller object as the delegate for the home model object
    self.filteredArray = [NSMutableArray arrayWithCapacity:[self.athletes count]];

#define ENABLE_SCOPE_BUTTONS 0

    // The table view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
    UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"TableSearchResultsNavController"];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

    self.searchController.searchResultsUpdater = self;

    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);

    self.listTableView.tableHeaderView = self.searchController.searchBar;

#if ENABLE_SCOPE_BUTTONS

    NSMutableArray *scopeButtonTitles = [[NSMutableArray alloc] init];
    [scopeButtonTitles addObject:NSLocalizedString(@"All", @"Search display controller All button.")];

    for (NSString *sport in [Athlete sportTypeNames]) 
        NSString *displayName = [Product displayNameForType:deviceType];
        [scopeButtonTitles addObject:displayName];
    

    self.searchController.searchBar.scopeButtonTitles = scopeButtonTitles;
    self.searchController.searchBar.delegate = self;

#endif

    //self.searchController.searchBar.scopeButtonTitles = scopeButtonTitles;

    self.searchController.searchBar.delegate = self;

      self.definesPresentationContext = YES;

;

    // Do any additional setup after loading the view, typically from a nib.

UISearchController.m

//
//  SearchResultsTableViewController.m
//  Sample-UISearchController
//
//  Created by James Dempsey on 9/20/14.
//  Copyright (c) 2014 Tapas Software. All rights reserved.
//

#import "SearchResultsTableViewController.h"
#import "DetailViewController.h"
#import "AthleteModel.h"

@implementation SearchResultsTableViewController

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return [self.searchResults count];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"SearchResultCell" forIndexPath:indexPath];
if (cell == nil) 
    Athlete *athlete = [self.searchResults objectAtIndex:indexPath.row];

    cell.textLabel.text = athlete.name;
    NSLog(@"CELL Called");

    return cell;




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    Athlete *athlete = [self.searchResults objectAtIndex:indexPath.row];
    DetailViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"DetailViewController"];
    self.presentingViewController.navigationItem.title = @"Search";
    vc.athlete = athlete;
    [self.presentingViewController.navigationController pushViewController:vc animated:YES];


-(void)viewDidLoad
    Athlete *athlete = [self.searchResults objectAtIndex:0];
    self.tableView = self.tableView;
    self.tableView.frame = CGRectMake(0, 0, 320, 480);
    NSLog(@"Name: %@", athlete.name);


@end

【问题讨论】:

愚蠢的问题:你是在设置数据源指针吗? numberOfRowsInSection 只调用 CellforRowAtIndexPath 【参考方案1】:

假设委托和数据源已设置 - 有或没有搜索栏 - 像 CellforRowAtIndexPath 这样的 UITableview 方法在此语句触发时按顺序依次执行

     [self.myTable reloadData]; 

只是想知道您是否有搜索栏/ SearchCONtroller - 搜索逻辑在哪里

         - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar  
       // ....

       [self.myTable reloadData]; 
         

【讨论】:

【参考方案2】:

您是否为表格视图设置了委托和数据源?

self.tableView.delegate = self;
self.tableView.dataSource = self;

或类似的东西。

【讨论】:

【参考方案3】:

试试这个

searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 
    if (searchController.searchResultsTableView == self.tableView)
    
        return ...;
    

    return ...;

参考链接How do I use the UISearchBar and UISearchDisplayController

【讨论】:

嗯,理论上这一切都很好,但这意味着改变我的整个项目。现在,我正在为我的 UISearchController 使用外部控制器。就像我上面说的,条目正在通过。 NSLog(@"行数:%@", self.searchResults.count);返回有效数字。 另外,这是针对 UISearchDisplayController 而不是 UISearchController。【参考方案4】:

最后,我刚刚切换回基于 UISearchDisplayController 的搜索。我们现在都很开心。

【讨论】:

你能上传你的示例项目吗? @codercat 我看看我能不能把东西搅在一起。【参考方案5】:

您的问题在于以下几行:

// The table view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
    UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"TableSearchResultsNavController"];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

您将UINavigationController 设为搜索结果控制器,而不是SearchResultsTableViewController。因此cellForRowAtIndexPath 没有被SearchResultsTableViewController 调用。我很惊讶它没有崩溃。这应该可以解决它:

// The table view controller is in a nav controller, and so the containing nav controller is the 'search results controller'
    UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"TableSearchResultsNavController"];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:[searchResultsController.viewControllers firstObject]];

【讨论】:

以上是关于iOS 上的 UISearchController 不调用 cellForRowAtIndexPath的主要内容,如果未能解决你的问题,请参考以下文章

iOS 上的 UISearchController 不调用 cellForRowAtIndexPath

如何跨 iOS 版本设置 UISearchController?

表单演示文稿上的 UISearchController

UISearchController 中的 searchBar 在 iPad 上的拆分视图上未正确显示

UISearchController 中的取消按钮在 iOS 13 中没有正确消失

iOS UISearchController:等到 UISearchController 在取消搜索后被解除