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