我有一个 UISearchBar,每当我开始输入它时,我都会收到以下崩溃报告[__NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance 0x6a9d2102012-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
我正在使用 Storyboard。由于缺乏声誉,我无法上传图片,因此我无法向您展示我的变量所关联的内容。
最佳答案
您的 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];
}
}
关于iphone - 开始输入时 UISearchBar 崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11415225/