UITableView中的UISearchController在搜索结果为空时崩溃

问题描述:

我在UITableViewController中有一个UISearchController。每次显示表格视图时,我都希望使用上次搜索的文本填充搜索栏。为此,我添加了viewDidAppear中的代码来检索最后一个搜索字符串并将UISearchController设置为活动状态。UITableView中的UISearchController在搜索结果为空时崩溃

每当我不输入任何搜索文本,一切工作正常。

当我输入搜索文本后,搜索第一次正常工作。但是,当我重新打开这个视图,并用最后一个搜索词填充UISearchController,并且如果新列表现在返回空结果(这可能是因为我的tableview中的列表是动态的),那么应用程序在cellForRowAtIndexPath中崩溃尝试将行存取到空的搜索结果数组中。

我把NSLogs放在很多地方,发现了一个奇怪的东西。即使UISearchController处于活动状态,numberOfRowsInSection中的相应条件也会返回FALSE,从而返回原始项目列表中的项目计数,而不是搜索的项目列表(现在为0)。

它调用numOfRowsInSection,当搜索控制器是积极的,而不是去“如果([_searchController isActive])”该函数的条款,它错误变为“其他”条款,获取原有名单伯爵(非搜索),它是0,然后调用cellForRowAtIndexPath indexPath.row = 0。奇怪的是,在cellForRowAtIndexPath,它确实去“if(_searchController.active)”子句内。显然有一些竞争条件或我错过了一些东西。

我很困惑为什么会发生这种情况。将不胜感激任何帮助。

@interface TaalListViewController() 

@property (strong, nonatomic) NSArray *m_searchedItemList; 
@property NSArray *m_itemList; 
@property (retain, nonatomic) UISearchController *searchController; 

@end 

@implementation TaalListViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    @try { 
     [super viewDidLoad]; 

     // Uncomment the following line to preserve selection between presentations. 
     // self.clearsSelectionOnViewWillAppear = NO; 

     // This is a global function declared and returns the items list. This works fine. 
     _m_itemList = [Globals getItemsList]; 

     // Search controller 
     _m_searchedItemList = [_m_itemList mutableCopy]; 

     self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
     self.searchController.searchBar.delegate = self; 
     self.searchController.searchResultsUpdater = self; 
     [self.searchController.searchBar sizeToFit]; 
     self.searchController.dimsBackgroundDuringPresentation = NO; 
     self.definesPresentationContext = YES; 
     self.tableView.tableHeaderView = self.searchController.searchBar; 
    } 
    @catch (NSException *exception) { 
     NSLog(@"Caught exception in viewDidLoad"); 
     NSLog(@"CRASH: %@", exception); 
     NSLog(@"Stack Trace: %@", [exception callStackSymbols]); 
    } 
} 


- (void) viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    // Get the last searched text and automatically set it in the search bar 
    NSString *str = [Globals getLastSearchedText]; 
    if ((str != nil) && (!(str.length == 0))) 
    { 
     NSLog(@"Setting seach bar active automatically"); 
     self.searchController.searchBar.text = str; 
     self.searchController.active = YES; 
     // [self.searchController.searchBar becomeFirstResponder]; 
    } 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    @try { 
     // Return the number of rows in the section. 
     if ([self.searchController isActive]) 
     { 
      NSLog(@"Number of searched item rows = %lu", (unsigned long)_m_searchedItemList.count); 
      return _m_searchedItemList.count; 
     } 
     else 
     { 
      NSLog(@"Number of regular rows = %lu", (unsigned long)_m_itemList.count); 
      return _m_itemList.count; 
     } 
    } 
    @catch (NSException *exception) { 
     NSLog(@"Caught exception in numberOfRowsInSection"); 
     NSLog(@"CRASH: %@", exception); 
     NSLog(@"Stack Trace: %@", [exception callStackSymbols]); 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    @try { 
     static NSString *CellIdentifier = @"ListTableCell"; 

     ListTableViewCell *cell = (ListTableViewCell *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

     // Configure the cell... 
     if (cell == nil) 
     { 
      cell = [[ListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

     DisplayListItem *item = nil; 

     if (self.searchController.active) 
     { 
      NSLog(@"Row %ld of search list", (long)indexPath.row); 
      item = (DisplayListItem *) [_m_searchedItemList objectAtIndex:indexPath.row]; 
     } 
     else 
     { 
      NSLog(@"Row %ld of regular list", (long)indexPath.row); 
      item = (DisplayListItem *) [_m_itemList objectAtIndex:indexPath.row]; 
     } 

     // Set the cell here and return 
     // ... 
     // Omitted code 
     // ... 

     return cell; 
    } 
    @catch (NSException *exception) { 
     NSLog(@"Caught exception in cellForRowAtIndexPath"); 
     NSLog(@"CRASH: %@", exception); 
     NSLog(@"Stack Trace: %@", [exception callStackSymbols]); 
    } 
} 

#pragma mark - UISearchResultsUpdating 

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController { 

    NSString *searchString = [self.searchController.searchBar text]; 

    NSLog(@"updateSearchResultsForSearchController called, searchString = %@", searchString); 

    _m_searchedItemList = [_m_itemList mutableCopy]; 

    if (searchString.length != 0) 
    { 
     [self filterContentForSearchText:searchString]; 
    } 

    [self.tableView reloadData]; 
} 

#pragma mark - UISearchBarDelegate 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    NSLog(@"searchBarSearchButtonClicked called"); 
    [searchBar resignFirstResponder]; 
} 

- (void)filterContentForSearchText:(NSString*)searchText 
{ 
    NSLog(@"filterContentForSearchText called"); 
    @try { 

     NSMutableArray *subPredicates = [NSMutableArray arrayWithCapacity:searchText.count]; 
     for (NSString *searchStr in searchText) 
     { 
      [subPredicates addObject:[NSPredicate predicateWithFormat:@"m_name contains[c] %@", searchStr]]; 
     } 
     NSPredicate *resultPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates]; 
     _m_searchedItemList = [_m_itemList filteredArrayUsingPredicate:resultPredicate]; 

     [Globals setLastSearchedText:searchText]; 

     NSLog(@"filterContentForSearchText finished, searched count = %lu", _m_searchedItemList.count); 
    } 
    @catch (NSException *exception) { 
     NSLog(@"Caught exception in filterContentForSearchText"); 
     NSLog(@"CRASH: %@", exception); 
     NSLog(@"Stack Trace: %@", [exception callStackSymbols]); 
    } 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    [Globals setLastSearchedText:searchText]; 
} 
@end 

这里是调试器中的NSLogs的输出。

2016-10-04 22:45:46.364927 AppName[1387:639346] Setting seach bar active automatically 
2016-10-04 22:45:46.365378 AppName[1387:639346] updateSearchResultsForSearchController called, searchString = Kay 
2016-10-04 22:45:46.365470 AppName[1387:639346] filterContentForSearchText called 
2016-10-04 22:45:46.366522 AppName[1387:639346] filterContentForSearchText finished, searched count = 0 
2016-10-04 22:45:46.366825 AppName[1387:639346] Number of regular rows = 215 
2016-10-04 22:45:46.367319 AppName[1387:639346] updateSearchResultsForSearchController called, searchString = Kay 
2016-10-04 22:45:46.367407 AppName[1387:639346] filterContentForSearchText called 
2016-10-04 22:45:46.368190 AppName[1387:639346] filterContentForSearchText finished, searched count = 0 
2016-10-04 22:45:46.368264 AppName[1387:639346] Number of regular rows = 215 
2016-10-04 22:45:46.371081 AppName[1387:639346] Row 0 of search list 
2016-10-04 22:45:46.373274 AppName[1387:639346] Caught exception in cellForRowAtIndexPath 
2016-10-04 22:45:46.373383 AppName[1387:639346] CRASH: *** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray 
2016-10-04 22:45:46.382029 AppName[1387:639346] Stack Trace: (
    0 CoreFoundation      0x0000000192d981d8 <redacted> + 148 
    1 libobjc.A.dylib      0x00000001917d055c objc_exception_throw + 56 
    2 CoreFoundation      0x0000000192d033dc <redacted> + 0 
    3 AppName        0x00000001001269bc -[ListViewController tableView:cellForRowAtIndexPath:] + 512 
    4 UIKit        0x0000000198f243d4 <redacted> + 716 
    5 UIKit        0x0000000198f24604 <redacted> + 80 
    6 UIKit        0x0000000198f11bac <redacted> + 2304 
    7 UIKit        0x0000000198f29668 <redacted> + 116 
    8 UIKit        0x0000000198cc5b14 <redacted> + 176 
    9 UIKit        0x0000000198bde54c <redacted> + 1196 
    10 QuartzCore       0x00000001960a640c <redacted> + 148 
    11 QuartzCore       0x000000019609b0e8 <redacted> + 292 
    12 QuartzCore 
2016-10-04 22:45:46.382306 AppName[1387:639346] -[_UITableViewReorderingSupport _needsSetup]: unrecognized selector sent to instance 0x170290e00 
2016-10-04 22:45:46.382714 AppName[1387:639346] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UITableViewReorderingSupport _needsSetup]: unrecognized selector sent to instance 0x170290e00' 
*** First throw call stack: 
(0x192d981c0 0x1917d055c 0x192d9f278 0x192d9c278 0x192c9659c 0x198d22c6c 0x198f24440 0x198f24604 0x198f11bac 0x198f29668 0x198cc5b14 0x198bde54c 0x1960a640c 0x19609b0e8 0x19609afa8 0x196017c64 0x19603f0d0 0x19603faf0 0x192d457dc 0x192d4340c 0x192d4389c 0x192c72048 0x1946f5198 0x198c4c628 0x198c47360 0x10015aaa8 0x191c545b8) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
+1

使用前初始化数组。 –

+0

我已经初始化了数组://这是一个声明的全局函数并返回项目列表。这工作正常。 _m_itemList = [Globals getItemsList]; //搜索控制器 _m_searchedItemList = [_m_itemList mutableCopy];这段代码已经在viewDidLoad中初始化了。 – Abhijit

+0

看到这种行为,当滚动时离开searchmode ...在numberOfRows searchController isActive是YES,当做cellForRowAtIndexPath(因为滚动),这是否... – Tobias

2016-10-04 22:45:46.373274 AppName[1387:639346] Caught exception in cellForRowAtIndexPath 
2016-10-04 22:45:46.373383 AppName[1387:639346] CRASH: *** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray 

说它有道理!在cellForRowAtIndexPath中试图访问元素0的一个空NSArray

不要这样做,它不会崩溃!

+0

我明白这一点。问题是 - 它调用numOfRowsInSection,而不是进入该函数的“if([_searchController isActive])”子句,它进入“else”子句,获得原始列表的count(未搜索),然后调用具有错误indexPath的cellForRowAtIndexPath。更奇怪的是,在cellForRowAtIndexPath中,它确实在“if(_searchController.active)”子句中正确执行。显然有一些竞争条件或我错过了一些东西。 – Abhijit

+0

我用上面的注释更新了我的问题。 – Abhijit