菜鸟教程小白 发表于 2022-12-13 04:44:38

ios - iOS 上的 UISearchController 不调用 cellForRowAtIndexPath


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

<p>TableViewController.m:</p>

<pre><code>    - (void)viewDidLoad {
    ;



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

    // Set this view controller object as the delegate for the home model object
    self.filteredArray = ];

#define ENABLE_SCOPE_BUTTONS 0

    // The table view controller is in a nav controller, and so the containing nav controller is the &#39;search results controller&#39;
    UINavigationController *searchResultsController = [ instantiateViewControllerWithIdentifier:@&#34;TableSearchResultsNavController&#34;];

    self.searchController = [ 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 = [ init];
    ;

    for (NSString *sport in ) {
      NSString *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.
}
</code></pre>

<p>UISearchController.m</p>

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

#import &#34;SearchResultsTableViewController.h&#34;
#import &#34;DetailViewController.h&#34;
#import &#34;AthleteModel.h&#34;

@implementation SearchResultsTableViewController

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ;
}

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

    UITableViewCell *cell = ;
if (cell == nil) {
    Athlete *athlete = ;

    cell.textLabel.text = athlete.name;
    NSLog(@&#34;CELL Called&#34;);
}
    return cell;


}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Athlete *athlete = ;
    DetailViewController *vc = [ instantiateViewControllerWithIdentifier:@&#34;DetailViewController&#34;];
    self.presentingViewController.navigationItem.title = @&#34;Search&#34;;
    vc.athlete = athlete;
    ;
}

-(void)viewDidLoad{
    Athlete *athlete = ;
    self.tableView = self.tableView;
    self.tableView.frame = CGRectMake(0, 0, 320, 480);
    NSLog(@&#34;Name: %@&#34;, athlete.name);
}

@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>假设委托(delegate)和数据源已设置 - 有或没有搜索栏 - 像 CellforRowAtIndexPath 这样的 UITableview 方法在此语句触发时依次执行</p>

<pre><code>   ;
</code></pre>

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

<pre><code>         - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
       // ....

       ;
         }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - iOS 上的 UISearchController 不调用 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27894307/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27894307/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - iOS 上的 UISearchController 不调用 cellForRowAtIndexPath