我想我找到了一個更好的實現這個問題。之前的所有答案都正確地顯示了與搜索前UITableView相似的灰色視圖,但是每個解決方案都缺少點擊該區域以取消搜索的功能。
因此,我認爲這個代碼效果更好。
首先,創建一個BOOL,如searchButtonTapped來指示是否點擊搜索按鈕。默認情況下它是NO。
然後:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
if (!searchButtonTapped) {
// To prevent results from being shown and to show an identical look to how the tableview looks before a search
[controller.searchResultsTableView setBackgroundColor:[UIColor clearColor]];
[controller.searchResultsTableView setRowHeight:160];
self.searchDisplayController.searchResultsTableView.scrollEnabled = NO;
} else {
// Restore original settings
[controller.searchResultsTableView setBackgroundColor:[UIColor whiteColor]];
[controller.searchResultsTableView setRowHeight:44];
self.searchDisplayController.searchResultsTableView.scrollEnabled = YES;
}
return YES;
}
這個現在應該基於他的答案是明確的。請確保在用戶點擊搜索按鈕時恢復原始設置。
此外,在cellForIndexPath方法增加:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.contentView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8];
爲了創建輸入文本之前所顯示的相同的變暗視圖。確保將這些屬性應用於正確的單元格,即檢查哪個UITableView處於活動狀態,並且用戶沒有點擊搜索按鈕。
然後,重要的是,在didSelectRowAtIndexPath方法:
if (tableView == self.searchDisplayController.searchResultsTableView) {
if (searchButtonTapped) {
// Code for when the user select a row after actually having performed a search
{
else
[self.searchDisplayController setActive:NO animated:YES];
現在,用戶可以點擊暗淡的區域,這會不會導致一個UITableViewCell的可見選擇,而是取消搜索。
正是我在找的東西。非常感謝! – Zargony 2009-10-20 12:42:00
第一次爲我工作,但如果你搜索,點擊x清除,然後輸入更多的文字,它顯示所有的行與不透明的黑色背景和白色的分界線。我猜它可能是在UITableCellViews,所以我說這個給你的功能和它的工作: 爲(UIView的*在self.searchDisplayController.searchResultsTableView.subviews子視圖){ \t \t [子視圖removeFromSuperview] } – mclin 2010-09-04 06:06:21