Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Objective-C中的搜索栏内按时,UISearchController会在安全区域内触摸_Ios_Objective C_Uitableview_Uisearchcontroller - Fatal编程技术网

Ios 在Objective-C中的搜索栏内按时,UISearchController会在安全区域内触摸

Ios 在Objective-C中的搜索栏内按时,UISearchController会在安全区域内触摸,ios,objective-c,uitableview,uisearchcontroller,Ios,Objective C,Uitableview,Uisearchcontroller,我有一个tableview,我以编程方式创建了一个UISearchController,并将其添加到tableview中。 起初它看起来不错,但当我在搜索栏内按下时,它看起来就在下面。 我不想在导航栏中保留我的搜索栏。我不能使用UITableViewController 找到下面我实现的代码 我的.h文件 @property (strong, nonatomic) UISearchController *searchController; NSMutableArray *tableDat

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

起初它看起来不错,但当我在搜索栏内按下时,它看起来就在下面。

我不想在导航栏中保留我的搜索栏。我不能使用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;
}
我想在tableview上方而不是naviationbar中显示我的搜索栏

提前谢谢。

我自己晚些回答。
我使用
uitextfield
而不是
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;
}  

为什么要使用UISearchController?只要使用搜索栏就可以避免上述问题,因为您正在内部视图中使用它。