我有一個tableview加載一個名爲self.annotationToSort的數組,並使用UISearchBar在tableview中搜索對象並更新結果。如果選擇了tableview中的項目,則會顯示一個詳細視圖以及相應的索引路徑信息。爲什麼UISearchBar tableview索引路徑總是返回objectAtIndex:0
我遇到的問題是,當使用搜索欄時,我在搜索中得到2個結果項,單元格中的值顯示正確,但然後我點擊結果B,我得到對象A傳遞給詳細視圖控制器。如果我點擊一個,我也得到A.
下面是相關代碼:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([self.annotationsToSort count] == 0) {
return 1;
} else if (tableView == self.searchDisplayController.searchResultsTableView) {
return [filteredResultsArray count];
} else {
return [self.annotationsToSort count];
}
}
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[self.filteredResultsArray removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SELF.name contains[c] %@) OR (SELF.ciudad contains[c] %@)", searchText,searchText];
[self.filteredResultsArray = [NSMutableArray arrayWithArray:[self.annotationsToSort filteredArrayUsingPredicate:predicate]] retain];
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
tableType = @"FILTER";
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSLog(@"indexpath selected %@", indexPath);
//Check if using normal or search array
if ([tableType isEqualToString:@"FILTER"]) {
MyLocation *sendingLocation = [self.filteredResultsArray objectAtIndex:indexPath.row];
DetailViewController *destinationViewController = segue.destinationViewController;
destinationViewController.receivedLocation = sendingLocation;
NSLog(@"indexpath selected filter %@", indexPath);
} else {
MyLocation *sendingLocation = [self.annotationsToSort objectAtIndex:indexPath.row];
DetailViewController *destinationViewController = segue.destinationViewController;
destinationViewController.receivedLocation = sendingLocation;
NSLog(@"indexpath selected regular %@", indexPath);
}
}
我可以看到被記錄的索引路徑爲空正在過濾的tableview時。它總是返回objectAtIndex:0。
什麼'NSLog(@「indexpath選擇%@」,indexPath);'輸出? – Wain
@我只是編輯了我的問題。在過濾器模式下,它將記錄爲空!但是tableview仍然顯示所有正確過濾的結果。 – marciokoko
神聖的巢嵌套蝙蝠俠。只要我遇到一個我不明白的問題,第一步(當然是在向StackOverflow發佈問題之前)將代碼重寫爲可讀的內容。 – nhgrif