ios - iOS 上的 UISearchController 不调用 cellForRowAtIndexPath

标签 ios objective-c uisearchcontroller

在我的 Xcode 项目中,我有一个带有元素的 TableView,我添加了一个带有 Apple 的“令人惊叹的新”UISearchController 的搜索栏。它运行良好了一点,但现在没有在 UISearchController 上调用“cellForRowAtIndexPath”。结果通过,我 NSLogg'ed 计数,但单元格未配置。知道为什么会这样吗?

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

最佳答案

假设委托(delegate)和数据源已设置 - 有或没有搜索栏 - 像 CellforRowAtIndexPath 这样的 UITableview 方法在被这个语句触发时按顺序一个接一个地执行

     [self.myTable reloadData]; 

只是想知道如果您有 Searchbar/SearchCONtroller - 搜索逻辑在哪里

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

       [self.myTable reloadData]; 
         }

关于ios - iOS 上的 UISearchController 不调用 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27894307/

相关文章:

ios - 在 App Delegate Xcode 5 中设置 View Controller

ios - 试图从 appdelegate 设置窗口

ios - 使用 UISearchController 的 segue 的不同行为

ios - 如何在 Swift 中打开搜索 tableView

ios - UIView 容器内的 MPMoviePlayerControllers View

objective-c - 如何以编程方式检测 iPad 的世代?

ios - CATransform3DRotate 后我的一半 View 消失了

ios - tableview 索引困惑

uitableview - UISearchController搜索栏隐藏tableview中的第一个单元格

ios - 尝试安装 Google 登录时出现链接错误