簡體   English   中英

UITableViewController中的UISearchController-Rows不可選,表視圖顯示為灰色

[英]UISearchController in a UITableViewController—Rows not selectable and table view is greyed out

我試圖讓UISearchController在UITableViewController中工作。 我以編程方式創建所有內容(即沒有界面構建器)。 除了在搜索時,我無法選擇任何表格單元格( -tableView:didSelectRowAtIndexPath:永遠不會被調用),所以一切都按-tableView:didSelectRowAtIndexPath:

但是,我不認為這是一個與代表有關的問題,也不認為該方法沒有被調用的另一個常見原因; 在搜索時,包含搜索結果的表格視圖顯示為灰色(即不活動),我相信這就是我無法選擇任何單元格的原因。

我的想法是UISearchBar被卡住了作為第一響應者,但我嘗試了各種方式來辭去這種狀態和/或試圖迫使表視圖成為第一響應者,但所有這些都帶來了災難性的結果。

這是我的UITableViewController子類的相關代碼:

@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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM