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

时间:2017-09-12 18:11:16

标签: ios objective-c

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

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

我正在关注此pattern

我遇到的问题是我跟随的模式将子文件中的数据加载到NSDict对象中,但我的数据在数组中。没问题,我只是修改了搜索以使用谓词(我现在硬编码了一些东西,得到名字中带有E的所有记录)。

但是我收到以下错误:

2017-09-12 13:01:41.538 Scoular [7644:1963822] - [employee isEqualToString:]:无法识别的选择器发送到实例0x608000105580 2017-09-12 13:01:41.604 Scoular [7644:1963822] ***由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [employee 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

1 个答案:

答案 0 :(得分:1)

updateFilteredContentForAirlineName函数中,您正在从self.employees数组中搜索可能包含employee自定义类对象的数组。因此,您的self.searchResults数组也包含相同的类对象。 但是,在您cellForRowAtIndexPath SearchResultsTableViewController 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; } 对象添加为字符串,导致您的应用崩溃。相反,你可以试试这个:

using (var client = new ScpClient(Config.UnixServer, Config.UnixUsername, Config.UnixPassword))
{
    client.Connect();
    client.Upload(new FileInfo(fileUpload), fileName);
    client.Disconnect();
}