原来的UITableView重新加载searchDisplayController

问题描述:

我有两个数据源为我的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 

您似乎认为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 
}