我有一個帶搜索欄的UITableView &搜索顯示控制器。我的代碼似乎是正確的,但是當我輸入搜索欄時,我沒有得到任何結果。 UITableView保持不變。searchDisplayController不更新UITableVIew
我的segue是正確的,因爲它仍然正確地將數據正確地傳遞給下一個VC的 。
推動正確的方向將不勝感激。
**注意:我意識到searchDisplayController從iOS8開始已棄用,但我想知道這一點。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [bands count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"BandCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [bands objectAtIndex:indexPath.row];
}
return cell;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [bands filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
編輯
我注意到,搜索欄wasnt連接。當我把它連接起來時,我注意到只要我在裏面挖掘,搜索欄就會消失。它看起來像搜索欄消失在導航欄後面。那是以後的另一個問題。
我仍然無法正確更新表格。
來源:Source
編輯
在一個側面說明,我想通了,爲什麼我的搜索欄被dissapearing。當我將導航欄嵌入到選項卡欄中時,我將我的選項卡欄嵌入到導航控制器中。
所以:標籤欄 - >導航控制器
添加以下代碼停止移動並重疊的狀態欄
我的搜索欄self.definesPresentationContext = YES;
編輯
不知道爲什麼但我注意到我的搜索欄/搜索控制器未正確連線。我刪除它,並將其添加回來,並正確連接所有東西。具體做法是:
searchDisplayController沒有有線高達樂隊名單。