2015-07-12 65 views
0

我有兩個數據源爲我的UITableView和我的SearchDisplayController的表視圖。我基於self.isSearching屬性在數組之間來回切換。這似乎工作正常,但是當我使用從搜索結果單元格抽頭推送的詳細視圖中的後退按鈕時,默認表視圖似乎在搜索結果表視圖下重新加載,這會導致出界限錯誤(因爲默認表中的結果比搜索結果表多)。原來的UITableView重新加載searchDisplayController

這張表爲什麼重新加載?當viewWillAppear回來時,我明確重新加載搜索表。

這對我來說似乎是一個錯誤,我會想象只有重點搜索結果表應該重新加載,如果有的話?如果我需要搜索結果數字,如何從numberOfRowsInSection返回0行結果?

這是我的方法和日誌。

- (NSMutableArray*)cachePointer 
{ 
    if (self.isSearching){ 
     return self.searchTitles; 
    }else{ 
     return self.titles; 
    } 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    NSLog(@"%@", NSStringFromSelector(_cmd)); 
    [super viewWillAppear:animated]; 

    if (self.isSearching) { 
     [self.searchResultsController.tableView reloadData]; 
    } else { 
     [self.tableView reloadData]; 
    } 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"%@ %ld", NSStringFromSelector(_cmd), [self.cachePointer count]); 
    return [self.cachePointer count]; 
} 

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSLog(@"%@, tableView: %@", NSStringFromSelector(_cmd), tableView); 
    NLSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"]; 

    NSInteger rowAtIndex = 0; 
    if (![self.cachePointer objectAtIndex:indexPath.row]){ 
     NSLog(@"[self.cachePointer objectAtIndex:%ld = nil", indexPath.row); 
     if (self.isSearching) { 
      [self updateSearchResultsForSearchController:self.searchController]; 
     } else { 
      [self primeTitleCache]; 
     } 
    } else { 
     NSLog(@"[self.cachePointer objectAtIndex:%ld = %ld", indexPath.row, [[self.cachePointer objectAtIndex:indexPath.row] integerValue]); 
     rowAtIndex = [[self.cachePointer objectAtIndex:indexPath.row] integerValue]; 
    } 

    if (!cell) { 
     NSLog(@"no cell to re-use. self.isSearching = %d", self.isSearching); 
     cell = [[NLSTableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:@"TitleCellIdentifier" 
       andId:rowAtIndex]; 
    } else { 
     NSLog(@"re-using cell at indexPath %ld, pulling %ld", indexPath.row, rowAtIndex); 
     [cell updateCellWithId:rowAtIndex]; 
    } 


    return cell; 
} 

2015-07-11 23:59:02.850 Colleen's EMA[8080:293895] viewWillAppear: 
2015-07-11 23:59:02.851 Colleen's EMA[8080:293895] cancelAllOperations 
2015-07-11 23:59:02.852 Colleen's EMA[8080:293895] tableView:numberOfRowsInSection: 1 
2015-07-11 23:59:02.855 Colleen's EMA[8080:293895] tableView:cellForRowAtIndexPath:, tableView: <UITableView: 0x7f984d847a00; frame = (0 0; 320 568); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7f984bd97e40>; layer = <CALayer: 0x7f984bf7dfc0>; contentOffset: {0, -108}; contentSize: {320, 148}> 
2015-07-11 23:59:02.856 Colleen's EMA[8080:293895] [self.cachePointer objectAtIndex:0 = 65398 
2015-07-11 23:59:02.856 Colleen's EMA[8080:293895] re-using cell at indexPath 0, pulling 65398 
2015-07-11 23:59:02.856 Colleen's EMA[8080:293895] updateCellWithId: 65398 
2015-07-11 23:59:02.856 Colleen's EMA[8080:293895] cancelAllOperations 
2015-07-11 23:59:02.856 Colleen's EMA[8080:293895] startQueryWithId: 65398 
2015-07-11 23:59:02.857 Colleen's EMA[8080:293895] initWithInvocation:andDelegate: <NSInvocation: 0x7f984f4773d0> 
2015-07-11 23:59:02.857 Colleen's EMA[8080:294381] NLSTMQuery main 
2015-07-11 23:59:02.857 Colleen's EMA[8080:293895] initWithInvocation:andDelegate: <NSInvocation: 0x7f984f562f00> 
2015-07-11 23:59:02.857 Colleen's EMA[8080:294381] SQLAPI getTitleModelForSQL: 
2015-07-11 23:59:02.864 Colleen's EMA[8080:294382] NLSJournalQuery main 
2015-07-11 23:59:02.864 Colleen's EMA[8080:293895] startDescriptorQuery: 65398 
2015-07-11 23:59:02.865 Colleen's EMA[8080:293895] initWithInvocation:andDelegate: <NSInvocation: 0x7f984eac6990> 
2015-07-11 23:59:02.865 Colleen's EMA[8080:294530] NLSDescriptorArrayQuery main 
2015-07-11 23:59:02.866 Colleen's EMA[8080:294530] getMeshDescriptorsForId: 
2015-07-11 23:59:02.868 Colleen's EMA[8080:293895] 

在這裏你可以看到底層的tableView作出的cellForRowAtIndexPath電話...

tableView:cellForRowAtIndexPath:, tableView: <UITableView: 0x7f984d825000; frame = (0 20; 320 1018); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x7f984bd145b0>; layer = <CALayer: 0x7f984bf7d050>; contentOffset: {0, -64}; contentSize: {320, 20995512}> 
    2015-07-11 23:59:02.884 Colleen's EMA[8080:293895] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 0]' 
    *** First throw call stack 

回答

0

您似乎認爲objectAtIndex:返回nil,如果該指數超出範圍:

if (![self.cachePointer objectAtIndex:indexPath.row]) 
{ 
    // index out of bounds, in fact this will never run 
} 

事實上,這不會返回零,而是如果索引超出範圍,則會引發異常。正確的方法檢查將是:

if (indexPath.row >= self.cachePointer.count) 
{ 
    // index out of bounds 
}