私はコアデータデータベースを持ち、各行はテーブルに表示されます。filteredArrayUsingPredicate配列が保持されない
私はユーザーが姓または名で検索できるようにする検索コントローラを持っています。
「filteredArrayUsingPredicate」から結果が得られたら、検索コントローラテーブルをリロードしても問題ありません。表には、フィルタリングされた結果が表示されます。
結果を選択するとき、結果配列は空であり、[配列atIndex:index path.row]が範囲外であるためにクラッシュします。配列は空で、私はそれが解放されたと思うようにします。
このコードは完全に別のビューで動作します。私は書き直しを試み、きれいにしてビルドしています。以下は
私のコードは
@property(アトミック、保持)にNSArray * cachedSearchResultsです。
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"first_name contains[cd] %@ || last_name contains[cd] %@",searchText, searchText];
self.cachedSearchResults = [[self.cachedResults filteredArrayUsingPredicate:predicate]retain];
;
[searchController.searchResultsTableView reloadData];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
PupilManagedObject* selected = nil;
if(tableView == table){
selected = [cachedResults objectAtIndex:indexPath.row];
}else{
selected = [self.cachedSearchResults objectAtIndex:indexPath.row]; //array is empty at this point
}
}
tableViewで配列を記録する:numberOfRowsInSectionとcellForRowAtIndexPathは、正しく配置された配列 – Dan