[UISearchController在Objective-C的搜索栏中按下时会触摸安全区域

问题描述 投票:0回答:1

我有一个tableview,我以编程方式创建了一个UISearchController,并将其添加到tableview中。Initial Search BAR

最初看起来不错,但是当我在搜索栏中按下时,它看起来下面。When I press search bar

我不想将搜索栏保留在导航栏中。而且我不能使用UITableViewController。

找到下面实现的代码。

我的.h文件

@property (strong, nonatomic) UISearchController *searchController;  
NSMutableArray *tableData;  
NSArray *filteredContentList;

我的.m文件

// New SearchController.
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.searchController.searchBar.searchBarStyle = UISearchBarStyleProminent;

    [self.searchController.searchBar sizeToFit];
    self.tblUserList.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;  

#pragma mark - UISearchDisplayDelegate

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = searchController.searchBar.text;
    NSPredicate *resultPredicate;

    //resultPredicate = [NSPredicate predicateWithFormat:@"displayname contains[c] %@",searchString];
    resultPredicate = [NSPredicate predicateWithFormat:@"value contains[c] %@", searchString];
    filteredContentList = [tableData filteredArrayUsingPredicate:resultPredicate];
    //filteredContentList = [[tableData valueForKey:@"data"] filteredArrayUsingPredicate:resultPredicate];
    [self.tblUserList reloadData];
}
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
    [self updateSearchResultsForSearchController:self.searchController];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    CGRect searchBarFrame = self.searchController.searchBar.frame;
    [self.tblUserList scrollRectToVisible:searchBarFrame animated:NO];
    return NSNotFound;
}

我想在表格视图上方显示我的搜索栏,而不是在导航栏中。

提前感谢。

ios objective-c uitableview uisearchcontroller
1个回答
0
投票

我自己最新的答案。我使用UITextFiled而不是UISearchBar。下面是我的代码实现。

[self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 

以及我的textFieldDidChange方法

-(void)textFieldDidChange:(UITextField *)textField
{
    searchTextString=textField.text;
    [self updateSearchArray:searchTextString];
}

-(void)updateSearchArray:(NSString *)searchText {
    if(searchText.length==0) {
        isFilter=NO;
    } else {
        isFilter=YES;
        filteredContentList = [[NSMutableArray alloc]init];
        NSPredicate *resultPredicate;
        resultPredicate = [NSPredicate predicateWithFormat:@"value contains[c] %@", searchText];
        filteredContentList = [tableData filteredArrayUsingPredicate:resultPredicate];
        [self.tblUserList reloadData];

    }
}  

以及我的TableView方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger numOfSections = 0;
    if ([filteredContentList count] || [tableData count]) {
        _tblUserList.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        numOfSections                = 1;
        _tblUserList.backgroundView = nil;
    } else {
        UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _tblUserList.bounds.size.width, _tblUserList.bounds.size.height)];
         noDataLabel.text = NSLocalizedString(@"No Users found", @"Message");
         noDataLabel.textColor = [UIColor blackColor];
         noDataLabel.textAlignment = NSTextAlignmentCenter;
         _tblUserList.backgroundView = noDataLabel;
         _tblUserList.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return numOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    /*if (isSearching) {
        return [filteredContentList count];
    }
    else {
        return [tableData count];
    }*/
    if (isFilter) {
        return [filteredContentList count];
    } else {
        return [tableData count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    //cell.textLabel.text = [[tableData objectAtIndex:indexPath.row] valueForKeyPath:@"data.displayname"];
    // Configure the cell...
    if (isFilter) {
        //cell.textLabel.text = [filteredContentList objectAtIndex:indexPath.row];
        cell.textLabel.text = [[filteredContentList objectAtIndex:indexPath.row] valueForKeyPath:@"data.displayname"];
    }
    else {
        cell.textLabel.text = [[tableData objectAtIndex:indexPath.row] valueForKeyPath:@"data.displayname"];
    }
    return cell;
}  

OutPut

© www.soinside.com 2019 - 2024. All rights reserved.