开始键入时UISearchBar崩溃

时间:2012-07-10 14:13:11

标签: iphone objective-c uitableview search uisearchbar

我有一个UISearchBar,每当我开始输入它时,我都会收到以下崩溃报告

[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x6a9d210 2012-07-10 14:46:52.956 MinePedia[275:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x6a9d210' * First throw call stack: (0x11f9052 0x15d5d0a 0x11faced 0x115ff00 0x115fce2 0xaad2 0x4754b2 0x11faec9 0x242515 0x2e872f 0x2e7e1e 0x2f4ce9 0x30112a 0xbf3a39 0x11c4885 0x11c47a8 0xb381aa 0x40a88e7 0x34b3917 0x3877111 0x387a4e1 0x408385b 0x40862e3 0x4086440 0x408301d 0x3869df0 0x3869ee3 0x4087119 0x388284d 0x3882b32 0x3896e12 0x3dfc0f7 0x3895245 0x38941f2 0x38948fb 0x3dfbca4 0x38a964e 0x38970a0 0x3879a0a 0x34e4ad9 0x11fae72 0x40a35bc 0x350b7f9 0x350d87f 0x3dfe03 0x3a3792 0x3a4944 0x3a36b6 0x3acf09 0x24e406 0x32c14a 0x32c14a 0x32c14a 0x32c14a 0x32c14a 0x32c14a 0x32c14a 0x32c14a 0x32c14a 0x32c14a 0x32c14a 0x32c14a 0x32c14a 0x24e460 0x24e0c5 0x24e1f8 0x241aa9 0x2172fa9 0x11cd1c5 0x1132022 0x113090a 0x112fdb4 0x112fccb 0x2171879 0x217193e 0x23fa9b 0x22f8 0x2255) terminate called throwing an exceptionCurrent language: auto; currently objective-c (gdb)

我的SearchTableViewController.h文件:

#import <UIKit/UIKit.h>

@interface SearchTableViewController : UITableViewController <UISearchBarDelegate> {

    NSMutableArray *serversArray;
    NSMutableArray *modsArray;
    NSMutableArray *pluginsArray;

    NSMutableArray *allItemsArray;
    NSMutableArray *displayItemsArray;

    IBOutlet UISearchBar *theSearchBar;
}
@end

和我的SearchTableViewController.m文件:

#import "SearchTableViewController.h"


@implementation SearchTableViewController

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

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

     // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    modsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://21zach2.webs.com/iPhone/MinePedia/Plists/TopRated/Mods.plist"]];
    serversArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://21zach2.webs.com/iPhone/MinePedia/Plists/TopRated/Servers.plist"]];
    pluginsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://21zach2.webs.com/iPhone/MinePedia/Plists/TopRated/Plugins.plist"]];

    allItemsArray = [[NSMutableArray alloc] init];
    displayItemsArray = [[NSMutableArray alloc] init];

    [allItemsArray addObjectsFromArray:modsArray];
    [allItemsArray addObjectsFromArray:serversArray];
    [allItemsArray addObjectsFromArray:pluginsArray];

    [displayItemsArray addObjectsFromArray:allItemsArray];

}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    if ([searchText length] == 0) {
        [displayItemsArray removeAllObjects];
        [displayItemsArray addObjectsFromArray:allItemsArray];
    } else {
        [displayItemsArray removeAllObjects];
        for (NSString *string in allItemsArray) {
            NSRange range = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (range.location != NSNotFound) {
                [displayItemsArray addObject:string];
            }
        }
    }
    [self.tableView reloadData];

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

    [searchBar resignFirstResponder];
    [theSearchBar resignFirstResponder];

}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

    [theSearchBar resignFirstResponder];
    [searchBar resignFirstResponder];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#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.
    return [displayItemsArray count];
}

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

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

    // Configure the cell...
    cell.textLabel.text = [[displayItemsArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

我正在使用故事板。由于缺乏声誉,我无法上传图片,所以我无法告诉你我的变量链接到了什么。

2 个答案:

答案 0 :(得分:5)

您的allItemsArray包含字典,而不是字符串。您需要获取要搜索的字符串值,例如,如果它是存储在键下的字符串&#34; name&#34;:

for (NSDictionary *item in allItemsArray) { 
    NSString *string = [item objectForKey:@"name"];            
    NSRange range = [string rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
    if (range.location != NSNotFound) { 
        [displayItemsArray addObject:item]; 
    } 
}

答案 1 :(得分:1)

for (int count = 0; count < [allItemsArray count]; count ++) { 

    for (NSDictionary *dict in [allItemsArray objectAtIndex:count]) { 

        NSString *string = [dict objectForKey:@"keyValue"];   

        NSRange range = [string rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

        if (range.location != NSNotFound) { 

            [displayItemsArray addObject:string]; 
        }
    } 
}

我希望这能解决它!!

相关问题