2012-08-23 64 views
2

我的應用程序崩潰後,如果我這樣做:應用程序崩潰的UITableView搜索

  1. 我做一個搜索我的UITableView

  2. 搜索我輕按searchDisplayController的UITableView的UITableViewCell的,去到另一個查看後控制器

  3. 然後我回到主要的UITableView,questionTable - 我在其中搜索,然後點擊最後一個單元格或任何單元格,這個單元格大於我以前的搜索結果數量。

  4. 應用程序崩潰與此錯誤:

    *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 12 beyond bounds [0 .. 0]' 
    *** First throw call stack:(0x35aa188f 0x37e48259 0x359ea9db 0x155fd 0x3384df5b 0x153ed 0x3358793d 0x33601627 0x355bb933 0x35a75a33 0x35a75699 0x35a7426f 0x359f74a5 0x359f736d 0x37693439 0x33503cd5 0x95a3 0x9548) terminate called throwing an exception(lldb) 
    

我懷疑,問題是我的代碼,因爲搜索的UITableView對我來說是一個新課題。這就是我要做的事:

1.My VC符合這些協議<UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>

2.我有這些變量:

@property (nonatomic, retain) NSMutableArray *searchResults; 
@property (nonatomic, copy) NSString *savedSearchTerm; 

3.在viewDidLoad設置我的UITableView的來源,我搜索:

self.questionList = [[NSArray alloc] 
        initWithObjects: MY OBJECTS HERE, nil]; 

if ([self savedSearchTerm]) 
{ 
    [[[self searchDisplayController] searchBar] setText:[self savedSearchTerm]]; 
} 

4.我有這樣的行動來處理搜索:

- (void)handleSearchForTerm:(NSString *)searchTerm 
{ 
[self setSavedSearchTerm:searchTerm]; 

if ([self searchResults] == nil) 
{ 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 
    [self setSearchResults:array]; 
} 
[[self searchResults] removeAllObjects]; 

if ([[self savedSearchTerm] length] != 0) 
{ 
    for (NSString *currentString in [self questionList]) 
    { 
     if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) 
     { 
      [[self searchResults] addObject:currentString]; 
     } 
    } 
} 
} 

5)這裏就是我建立了我的UITable觀點:

(UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    NSInteger row = [indexPath row]; 
    NSString *contentForThisRow = nil; 

    if (tableView == [[self searchDisplayController] searchResultsTableView]) 
     contentForThisRow = [[self searchResults] objectAtIndex:row]; 
    else 
     contentForThisRow = [[self questionList] objectAtIndex:row]; 

    static NSString *CellIdentifier = @"questionsCell"; 

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

6)這是我的numberOfRowsInSection方法看起來像:

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
NSInteger rows; 

if (tableView == [[self searchDisplayController] searchResultsTableView]) 
rows = [[self searchResults] count]; 
else 
rows = [[self questionList] count]; 
return rows; 
} 

7)最後,我有以下兩種方法:

(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self handleSearchForTerm:searchString]; 
    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 

(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller 
{ 
    [self setSavedSearchTerm:nil]; 
    [[self questionTable] reloadData]; 
} 

對於長期問題,我真的很抱歉。我在代碼中犯了一些錯誤,我希望有人能告訴我正確的做法。搜索欄控制器正確鏈接到IB 任何幫助將不勝感激!

新的編輯: 我在didSelectRowAtIndePath推一個新的VC:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self performSegueWithIdentifier:@"toAnswer" sender:indexPath]; 
} 

而且我更新一個NSString那裏。它工作完美時,我只用一個TableView

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    ((QandADetailsViewController *)segue.destinationViewController).delegate=self; 

    NSIndexPath *indexPath = (NSIndexPath *)sender; 

    QandADetailsViewController *mdvc = segue.destinationViewController; 

    if (self.searchResults == nil) { 
     mdvc.questionName = [self.questionList 
          objectAtIndex:indexPath.row]; 
    } else { 
     mdvc.questionName = [self.searchResults 
          objectAtIndex:indexPath.row]; 
    } 
} 
+0

你可以發佈didSelectRowAtIndexPath:?聽起來就像是碰撞發生的地方?爲了確定,請設置一個所有例外斷點(左側的項目導航器,頂部的斷點標籤,'+'加號,底部的'添加異常斷點')。請評論它停止的行。 – danh

+0

問題是您的數組爲零,或爲空 – MCKapur

+0

將主數組中的元素添加到searchArray時,是否使用了深層複製?第二,當你回來時,請在視圖中重新加載表格,然後應用程序崩潰或不?告訴我。 –

回答

3

在對SEGUE準備,爲SearchResult所檢查==零可能不正確。沒有理由期望它在第一次搜索後永遠不會有。這意味着您將始終在隨後的表格選擇中取消引用搜索數組,解釋索引超出範圍。

我認爲這個修復方法是詢問if ([self.searchDisplayController isActive])而不是搜索結果是否存在。

+0

當我嘗試實現代碼時出現此錯誤:''UISearchDisplayController'沒有可見的@interface聲明選擇器'active '有沒有解決辦法? –

+0

應該是'if([self.searchDisplayController isActive])',它的工作原理!非常感謝你,男人! –

+0

很高興聽到。對不起,這個錯誤。我認爲主動是一個屬性,可以用點來訪問。我會根據你的建議進行編輯。 – danh

1

看來你的數組是空的。

我會建議你在你做的每一步之後記錄你的數組,所以你可以看到它在哪裏變空。

也記錄了行號和方法名稱,看到這種情況,如何登錄:

NSLog(@"%s\n [Line: %d]\n array:%@\n\n", __PRETTY_FUNCTION__, __LINE__, yourArray);