2017-09-12 38 views
0

我有一個爲iOS 7編寫的應用程序,我需要更新,因爲UISearchDisplayController已棄用UISearchController。從UISearchDisplayController轉換==> UISearchController無法顯示搜索欄

我已經通過了幾個教程,並嘗試過,但我無法得到這個工作,但我很接近。

我跟隨此pattern

我遇到的問題是我下面的模式將子文件中的數據加載到NSDict對象中,但是我的數據在數組中。沒問題,我只是修改了搜索以使用謂詞(我現在硬編碼了一些東西,所有記錄都帶有E中的名字)。

但我收到以下錯誤:

2017年9月12日13:01:41.538 Scoular [7644:1963822] - [員工isEqualToString:]:無法識別的選擇發送到實例0x608000105580 2017-09-由於未捕獲的異常'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

在你updateFilteredContentForAirlineName功能,您是從self.employees數組這可能包含employee自定義類對象搜索。出於這個原因,你的self.searchResults數組也包含相同的類對象。 但是在你的cellForRowAtIndexPathSearchResultsTableViewController你正在做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; 
} 
相關問題