gpt4 book ai didi

ios - 从 UISearchDisplayController 转换 ==> UISearchController 无法显示搜索栏

转载 作者:行者123 更新时间:2023-11-29 00:12:08 25 4
gpt4 key购买 nike

我有一个为 iOS 7 编写的应用程序,我需要更新,因为 UISearchDisplayController 已被弃用。

我已经完成了几个教程并尝试过,但我无法让它工作,但我已经很接近了。

我正在关注这个 pattern .

我遇到的问题是我遵循的模式将数据从子文件加载到 NSDict 对象中,但我的数据在数组中。没问题,我只是修改了搜索以使用谓词(我现在硬编码了一些内容,获取名称中带有 E 的所有记录)。

但我收到以下错误:

2017-09-12 13:01:41.538 Scoular[7644:1963822] -[employee isEqualToString:]:无法识别的选择器发送到实例 0x6080001055802017-09-12 13:01:41.604 Scoular[7644:1963822] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[员工 isEqualToString:]:无法识别的选择器发送到实例 0x608000105580”

在 SearchResultsTableViewController.m 文件中,在行

 cell.textLabel.text = self.searchResults[indexPath.row];

来自下面的 m 文件。我很困惑。任何帮助将不胜感激。

#import "SearchResultsTableViewController.h"

@interface SearchResultsTableViewController ()

@property (nonatomic, strong) NSArray *array;

@end

@implementation SearchResultsTableViewController

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [self.searchResults count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultCell" forIndexPath:indexPath];

cell.textLabel.text = self.searchResults[indexPath.row];

return cell;
}
@end

头文件

@import UIKit;

@class EmployeeDetailViewController;

#import <CoreData/CoreData.h>
#import "EmployeeDatabase.h"

@interface EmployeeListViewController : UITableViewController

@end

执行文件

#import "EmployeeListViewController.h"
#import "EmployeeDetailViewController.h"
#import "SearchResultsTableViewController.h"

@interface EmployeeListViewController () <UISearchResultsUpdating>

@property (nonatomic, strong) NSMutableArray *employees;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResults;
@property (nonatomic,retain) NSMutableDictionary *sections;
@end

@implementation EmployeeListViewController

BOOL isSearching;


//Do I still need this
- (void)awakeFromNib {
if ([[UIDevice currentDevice] userInterfaceIdiom] ==
UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
[super awakeFromNib];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}


- (void)viewDidLoad {

[super viewDidLoad];

// Get array of employees and sections
self.employees = [EmployeeDatabase getEmployees];



self.sections = [EmployeeDatabase getSections:_employees];


// There's no transition in our storyboard to our search results tableview or navigation controller
// so we'll have to grab it using the instantiateViewControllerWithIdentifier: method

UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"TableSearchResultsNavController"];

// Our instance of UISearchController will use searchResults
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

// The searchcontroller's searchResultsUpdater property will contain our tableView.
self.searchController.searchResultsUpdater = self;

// The searchBar contained in XCode's storyboard is a leftover from UISearchDisplayController.
// Don't use this. Instead, we'll create the searchBar programatically.
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.tableView.tableHeaderView = self.searchController.searchBar;


// Set the back bar button
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithTitle:@"Employees"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;

}



#pragma mark - Table Sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger tmpCount;
if (isSearching) {
tmpCount = 1;
} else {
tmpCount = [[self.sections allKeys] count];
}
return tmpCount;
}

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

#pragma mark - Table View


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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}


employee *thisEmployee =
[[self.sections
valueForKey:[[[self.sections allKeys]
sortedArrayUsingSelector:
@selector(localizedCaseInsensitiveCompare:)]
objectAtIndex:indexPath.section]]
objectAtIndex:indexPath.row];
cell.textLabel.text = thisEmployee.fulNme;


return cell;
}

- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {

NSString *tmpString;
//if (tableView == self.searchDisplayController.searchResultsTableView) {
//tmpString = nil;
//} else {
tmpString =
[[[self.sections allKeys]
sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:
)] objectAtIndex:section];
//}
return tmpString;
}

#pragma mark - Right side bar alphabetical index
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

NSArray *tmpTitle;
if (isSearching) {
tmpTitle = nil;
} else {
tmpTitle = [[self.sections allKeys]
sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
return tmpTitle;
}


#pragma mark - UISearchControllerDelegate & UISearchResultsDelegate

// Called when the search bar becomes first responder
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{

// Set searchString equal to what's typed into the searchbar
NSString *searchString = self.searchController.searchBar.text;
[self updateFilteredContentForAirlineName:searchString];

// If searchResultsController
if (self.searchController.searchResultsController) {

UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;

// Present SearchResultsTableViewController as the topViewController
SearchResultsTableViewController *vc = (SearchResultsTableViewController *)navController.topViewController;

// Update searchResults
vc.searchResults = self.searchResults;

// And reload the tableView with the new data
[vc.tableView reloadData];
}
}


// Update self.searchResults based on searchString, which is the argument in passed to this method
- (void)updateFilteredContentForAirlineName:(NSString *)employeeName
{

if (employeeName == nil) {

// If empty the search results are the same as the original data
self.searchResults = [self.employees mutableCopy];

} else {

NSArray *searchResults2 = [[NSMutableArray alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"fulNme contains 'E'"];
searchResults2 = [self.employees filteredArrayUsingPredicate:predicate];
self.searchResults = [searchResults2 mutableCopy];

}
}

@end

最佳答案

在您的 updateFilteredContentForAirlineName 函数中,您正在从可能包含 employee 自定义类对象的 self.employees 数组中进行搜索。因此,您的 self.searchResults 数组也包含相同的类对象。但是在 SearchResultsTableViewControllercellForRowAtIndexPath 中,您正在执行 cell.textLabel.text = self.searchResults[indexPath.row]; 这意味着您正在添加employee 对象作为 cell.textLabel 中的字符串导致您的应用程序崩溃。相反,你可以试试这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchResultCell" forIndexPath:indexPath];
employee *thisEmployee = self.searchResults[indexPath.row];
cell.textLabel.text = thisEmployee.fulNme;

return cell;
}

关于ios - 从 UISearchDisplayController 转换 ==> UISearchController 无法显示搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46182979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com