• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - iOS 上的 UISearchController 不调用 cellForRowAtIndexPath

[复制链接]
菜鸟教程小白 发表于 2022-12-13 04:44:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我的 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)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
    return [self.searchResults count];
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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



Best Answer-推荐答案


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

     [self.myTable reloadData]; 

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

         - (void)searchBarSearchButtonClickedUISearchBar *)searchBar  {
       // ....

       [self.myTable reloadData]; 
         }

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap