我正在使用搜索栏来抓取用户输入的文本,然后使用它来过滤 NSArray,然后将新构造的数组分配给指定类的数组属性。下面的代码:
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",searchController.searchBar.text];
NSArray *temp = [_array filteredArrayUsingPredicate:predicate];
_resultViewController.dynamicArray = temp;
}
但是我的 resultViewController 的 tableView 没有显示任何数据。但是,如果我使用以下内容,它可以工作:
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSArray *temp = [[NSArray alloc]initWithObjects:@"hello",@"bye",@"why",@"okay", nil];
_resultViewController.dynamicArray = temp;
}
我不太确定如何解决这个问题。当我将 _resultViewController 的动态数组分配给 temp 时,似乎在使用过滤数组时尚未设置 temp。但它在第二种情况下工作正常,所以我知道其余的代码是正确的。
想法?