1

我有一个UISearchDisplayController一个IUSearchBar,一切运作良好:奇怪UISearchDisplayController崩溃隐藏键盘

  • 当我接触到搜索栏,键盘显示出来,该TableView中有一个黑色的封面上,并我可以开始输入

screenshot 1 http://joeranbosma.nl/xcode/sdc1.png

  • 当我输入的东西出现的清除按钮(X),所以做我的搜索结果。

screenshot 1 http://joeranbosma.nl/xcode/sdc2.png

  • 这工作一切伟大的。当我点击清除(x)按钮,它只是返回到第一个屏幕截图状态(空搜索栏,结果上方的黑框)

但是,它来了!

  • 当我输入一些东西,然后移动搜索结果,键盘隐藏(标准UISearchDisplayController事) 像这样:

screenshot 1 http://joeranbosma.nl/xcode/sdc3.png

当我然后一下就清楚(x)按钮,该程序崩溃与SIGABRT错误:

screenshot 1 http://joeranbosma.nl/xcode/sdc4.png

..我不知道如何解决这一问题:(

这里是我的源代码:(不知道你需要解决的问题是什么) http://joeranbosma.nl/xcode/wearch_stack.zip

但我认为这是最有用的部分:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //Initialize the array. 
    listOfItems = [[NSMutableArray alloc] init]; 
    copyListOfItems = [[NSMutableArray alloc] init]; 
    staticlist = [[NSMutableArray alloc] init]; 

    staticlist = listOfItems; 

    //Add items 
    [listOfItems addObject:@"Iceland"]; 
    [listOfItems addObject:@"Greenland"]; 
    [listOfItems addObject:@"Switzerland"]; 
    [listOfItems addObject:@"Norway"]; 
    [listOfItems addObject:@"New Zealand"]; 
    [listOfItems addObject:@"Greece"]; 
    [listOfItems addObject:@"Rome"]; 
    [listOfItems addObject:@"Ireland"]; 

    //Set the title 
    self.navigationItem.title = @"Wearch"; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 
// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 
// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (searching) 
     return [copyListOfItems count]; 
    else { 
     return [listOfItems count]; 
    } 
} 
// Customize the appearance of table view cells. 
- (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] autorelease]; 
    } 

    // Configure the cell. 
    if(searching) 
     cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row]; 
    else { 
     NSString *txtLbl = [listOfItems objectAtIndex:indexPath.row]; 
     cell.textLabel.text = [txtLbl stringByAppendingString:@"x"]; 
    } 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    //Get the selected word 

    NSString *selectedWord = nil; 

    if(searching) 
     selectedWord = [copyListOfItems objectAtIndex:indexPath.row]; 
    else { 
     selectedWord = [listOfItems objectAtIndex:indexPath.row]; 
    } 

    //Initialize the detail view controller and display it. 
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]]; 
    dvController.selectedWord = selectedWord; 
    [self.navigationController pushViewController:dvController animated:YES]; 
    [dvController release]; 
    dvController = nil; 
} 
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar { 
    searching = YES; 
} 
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText { 
    //Remove all objects first. 
    [copyListOfItems removeAllObjects]; 

    if([searchText length] > 0) { 
     searching = YES; 
     [self searchTableView]; 
    } 
    else { 
     searching = NO; 
    } 

    [self.tableView reloadData]; 
} 
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar { 
    [self searchTableView]; 
} 
- (void) searchTableView { 

    NSString *searchText = searchBar.text; 
    NSMutableArray *searchArray = [[NSMutableArray alloc] init]; 

    NSMutableArray *results1 = [[NSMutableArray alloc] init]; 
    NSMutableArray *results2 = [[NSMutableArray alloc] init]; 

    [searchArray addObjectsFromArray:listOfItems]; 

    for (NSString *sTemp in searchArray) { 
     NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

     if (titleResultsRange.length > 0){ 
      if (titleResultsRange.location == 0) { 
       [results1 addObject:sTemp]; 
      } 
      else{ 
       [results2 addObject:sTemp]; 
      } 
     } 
    } 

    for (NSString *sTemp in results1) { 
     [copyListOfItems addObject:sTemp]; 
    } 
    for (NSString *sTemp in results2) { 
     [copyListOfItems addObject:sTemp]; 
    } 

    [searchArray release]; 
    searchArray = nil; 
} 

我希望你们其中一个能解决这个问题。

你也可以说如果你喜欢的图片?

回答

4

这是一个有趣的。长的回答是,在tableView:numberOfRowsInSection:之后但在tableView:cellForRowAtIndexPath:之前调用searchBarTextDidBeginEditing:。结果是tableView期望[listOfItems count]行数,但在调用tableView:cellForRowAtIndexPath:searching BOOL为YES,因此视图控制器使用copyListOfItems来填充表,当然这些表中没有足够数量的对象来填充表。

简短的回答是,停止告诉正在搜索的视图控制器,直到您搜索;设置searching = YES;时有实际的文本您正在使用搜索,而不仅仅当searchBar是第一响应者。

真正答案很简单:

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar { 
    // Comment this assignment out 
    //searching = YES; 
} 

这似乎很好地工作。

一种这样的情景小贴士:

1)看在控制台打印堆栈跟踪。它必须让你看到的问题是:

'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 

2)使用NSLog小号大汗;我找到最有用的是:

NSLog(@"%@",NSStringFromSelector(_cmd)); 

编辑在应对非symbolicated堆栈跟踪在控制台(即0x14d3ec9 0x36e5c2):

3)使用异常断点;如右图:

enter image description here

4)添加未捕获的异常处理程序; As described here

+0

哇,非常感谢! 确实是这个问题。 但是,除了您发现问题之外,我现在更了解该语言。 我从来没有看过的控制台,因为当时很多这样的: '422af1 0x14d3ec9 0x36e5c2 0x36e55a 0x413b76 0x41403f 0x4132fe 0x393a30 0x393c56 0x37a384 0x36daa9 0x1bc8fa9 0x14a61c5 0x140b022 0x140990a 0x1408db4 0x1408ccb 0x1bc7879 0x1bc793e 0x36ba9b 0x256d 0x24e5)' 我只是没”我知道那里有些有用的东西,所以我没有看得更远。 所以:非常感谢! – Joeran 2012-03-26 06:17:02

+1

很高兴为您提供帮助,特别是想要了解更多信息的人。因此,我添加了一些技巧来帮助您提到的无用的十六进制堆栈跟踪。 – NJones 2012-03-27 15:14:06

+0

谢谢你,我现在要添加一个异常处理程序:) – Joeran 2012-03-27 17:59:43