OGeek|极客世界-中国程序员成长平台

标题: ios - 搜索 View 始终显示在表格 View 的顶部 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:07
标题: ios - 搜索 View 始终显示在表格 View 的顶部

我拿了一个 UITableViewController 并尝试在其中添加 UISearchDisplayController。我从互联网上阅读了很多代码,但不知道我有什么问题。

我的问题是为什么 UISearchDisplayController 不总是停留在 UITableView 之上。

这是我的代码

SearchView_controller.h

@interface SearchView_controller : UITableViewController<UISearchBarDelegate,UISearchDisplayDelegate>
@end


SearchView_controller.m

@interface SearchView_controller ()
{
    UISearchDisplayController *searchController;
}
@end

@implementation SearchView_controller

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title=@"Test";

    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, self.tableView.frame.origin.y, self.tableView.frame.size.width, 44)];
    [searchBar sizeToFit];
    [searchBar setDelegate:self];

    searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
                                                                contentsController:self];
    [searchController setSearchResultsDataSource:self];
    [searchController setSearchResultsDelegate:self];
    [searchController setDelegate:self];

    [self.tableView setTableHeaderView:searchController.searchBar];

}

-(void) scrollViewDidScrollUIScrollView *)scrollView {
    UISearchBar *searchBar = searchController.searchBar;
    CGRect rect = searchBar.frame;
    rect.origin.y = MAX(0, scrollView.contentOffset.y);
    searchBar.frame = rect;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView {
    return 1;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
    return 30;
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"searchview_customecell";
    searchview_customecell *cell = (searchview_customecell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.lbl_name.text=@"test";
    return cell;
}

searchview_customecell.h


@interface searchview_customecell : UITableViewCell
@property(nonatomic, weak) IBOutlet UILabel *lbl_name;
@end

searchview_customecell.m

@implementation searchview_customecell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelectedBOOL)selected animatedBOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end



Best Answer-推荐答案


最好用 UIViewController 而不是 UITableviewController 来处理这些东西。

UITableViewController 的情况下, View 是 tableView 所以每当添加 searchBar 时,它都会添加到 tableView .

这个问题有两种可能的解决方案:

  1. 要么用 UIViewController 替换 Tableviewcontroller 并将 Searchbar 放在 View 的开头(不要将其添加到 tableview 标题中),然后是 tableview。

  2. 点击此链接:UISearchDisplayController with UITableViewController

关于ios - 搜索 View 始终显示在表格 View 的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29821860/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4