【问题标题】:UISearchBar Crash When Start Typing开始输入时 UISearchBar 崩溃
【发布时间】:2012-07-10 14:13:10
【问题描述】:

我有一个 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

我正在使用故事板。由于缺乏声誉,我无法上传图片,所以我无法向您展示我的变量与什么相关联。

【问题讨论】:

  • 设置异常断点(转到断点导航器,添加新的,所有异常)。这将向您显示崩溃的行,但从您的代码看来, allItemsArray 中的一个对象是字典,而不是字符串。
  • 其实看cellForRowAtIndexPath,肯定是这样的。您期望那里有一本字典,但在 searchBar:textDidChange: 中将其视为字符串
  • 它从在线的 plist 中提取字典数组,但是带有 ket "name" 的行是一个字符串

标签: iphone objective-c uitableview search uisearchbar


【解决方案1】:

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

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

【讨论】:

  • 它是你循环的替代品,从for (NSString *string in allItemsArray) {开始
  • 好的,现在我的线路崩溃了,cell.textLabel.text = [[displayItemsArray objectAtIndex:indexPath.row] objectForKey:@"name"];
  • 哎呀 - 应该是 addObject:item 在最后一行 - 我已经更新了答案。
  • 您还需要 5 个声望点。不用担心,得到您需要的答案更重要。
  • 如果我将范围添加到搜索栏,相同的代码会影响它吗?
【解决方案2】:
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]; 
        }
    } 
}

我希望这能解决它!

【讨论】:

  • 我可以用这个替换我的另一个 for 循环吗
猜你喜欢
  • 2015-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多