Ios UITableViewController中的UISearchController—;行不可选择,表格视图变灰
我正在尝试使UISearchController在UITableViewController中工作。我以编程方式创建所有内容(即没有界面生成器)。大多数情况下,一切都正常工作,只是在搜索时,我无法选择任何表格单元格(Ios UITableViewController中的UISearchController—;行不可选择,表格视图变灰,ios,objective-c,ipad,ios8,uisearchcontroller,Ios,Objective C,Ipad,Ios8,Uisearchcontroller,我正在尝试使UISearchController在UITableViewController中工作。我以编程方式创建所有内容(即没有界面生成器)。大多数情况下,一切都正常工作,只是在搜索时,我无法选择任何表格单元格(-tableView:didselectrowatinexpath:从未被调用) 但是,我不认为这是一个与委托相关的问题,也不认为是该方法未被调用的另一个常见原因;搜索时,包含搜索结果的表视图显示为灰色(即不活动),我认为这就是我无法选择任何单元格的原因 我的想法是,UISearch
-tableView:didselectrowatinexpath:
从未被调用)
但是,我不认为这是一个与委托相关的问题,也不认为是该方法未被调用的另一个常见原因;搜索时,包含搜索结果的表视图显示为灰色(即不活动),我认为这就是我无法选择任何单元格的原因
我的想法是,UISearchBar作为第一响应者被卡住了,但我尝试了各种方法放弃这种状态和/或试图迫使表视图成为第一响应者,但所有这些都带来了灾难性的后果
以下是myUITableViewController
子类中的相关代码:
@interface SearchResultsTableViewController()
@property (copy, nonatomic) NSArray *searchResults;
@property (strong, nonatomic) UISearchController *searchController;
@end
@implementation SearchResultsTableViewController
- (NSArray *)searchResults
{
// Lazy array instantiation
if (!_searchResults)
{
_searchResults = [[NSArray alloc] init];
}
return _searchResults;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setSearchController:[[UISearchController alloc] initWithSearchResultsController:nil]];
[[self searchController] setSearchResultsUpdater:self];
[[[self searchController] searchBar] setFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth([[self tableView] frame]), 44.0f)];
[[[self searchController] searchBar] setSearchBarStyle:UISearchBarStyleMinimal];
[[[self searchController] searchBar] setPlaceholder:@"Search Presentations"];
[[[self searchController] searchBar] setDelegate:self];
[[self tableView] setTableHeaderView:[[self searchController] searchBar]];
[[self tableView] setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[[self tableView] setAllowsSelection:YES];
[[self tableView] setAllowsSelectionDuringEditing:YES];
[[self tableView] setDelegate:self];
[[self tableView] setDataSource:self];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self searchResults] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Search Results Cell"];
[cell setSelectionStyle:UITableViewCellSelectionStyleDefault];
[cell setUserInteractionEnabled:YES];
[[cell textLabel] setEnabled:YES];
FXIPresentation *presentation = nil;
presentation = [[self searchResults] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[[presentation title] substringFromIndex:3]];
[[cell textLabel] setTextColor:[UIColor colorWithRed:(18.0/255.0)
green:(52.0/255.0)
blue:(88.0/255.0)
alpha:1.0f]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Omitted / Never called
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
[self setSearchResults:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title contains[c] %@", [[[self searchController] searchBar] text]];
[self setSearchResults:[[self fullResults] filteredArrayUsingPredicate:predicate]];
[[self tableView] reloadData];
}
@end
也许是因为在您正在搜索的相同视图中显示搜索结果?尝试阻止视图变暗
self.searchController.dimsBackgroundDuringPresentation = NO;
这正是问题所在。我感到羞耻,因为API文档特别提到在同一视图中搜索/显示时需要此代码。非常感谢你!