按下一个字母时,UISearchBar应用程序崩溃了

时间:2013-12-10 09:47:13

标签: ios7 uisearchbar

嘿伙计们。我有一个问题,当我按下搜索栏中的一封信时,应用程序崩溃了。当我按一个字母terminating with uncaught exception of type NSException

时,我没有错误只有这种威胁

请在示例代码中提供帮助。

#import "BIDFirstLevelViewController.h"
#import "BIDSecondLevelViewController.h"
#import "MangaliaViewController.h"
#import "Country.h"
#import "LimanuViewController.h"

@interface BIDFirstLevelViewController ()<UISearchDisplayDelegate>

@property (strong, nonatomic) NSArray *controllers;
@property (nonatomic, strong) NSMutableArray *searchResults;

@end

@implementation BIDFirstLevelViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization

        self.title = @"Plecare";

    }
    return self;
}

- (void)viewDidLoad
{   
    [super viewDidLoad];

    self.controllers = [@[
                         [[MangaliaViewController alloc] init],
                         [[LimanuViewController alloc] init],

                         ]mutableCopy];

    self.searchResults = [NSMutableArray arrayWithCapacity:[self.controllers count]];  
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ 
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        return [self.searchResults count];
    } else {
        return [self.controllers count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    BIDSecondLevelViewController *controller = self.controllers[indexPath.row];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = controller.title;
    }

    UIImage *image = [UIImage imageNamed:@"bus.gif"];
    cell.imageView.image = image;

    return cell;
}

#pragma mark UISearchDisplay delegate

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    [self.searchResults removeAllObjects];

    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];

    self.searchResults = [NSMutableArray arrayWithArray:[self.controllers filteredArrayUsingPredicate:resultPredicate]];//[self.tableData filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BIDSecondLevelViewController *controller = self.controllers[indexPath.row];
    [self.navigationController pushViewController:controller animated:YES];
}

@end

1 个答案:

答案 0 :(得分:0)

您需要更改方法 searchDisplayController 的代码,然后重新执行以下代码来搜索数据和删除filterContentForSearchText 方法

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:
                                    @"SELF contains[cd] %@", searchString];

    [self.searchResults removeAllObjects];
    self.searchResults = [[NSMutableArray alloc] initWithArray:[self.controllers filteredArrayUsingPredicate:resultPredicate]];

    return YES;
}