• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 搜索 View 始终显示在表格 View 的顶部

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:07:51 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我拿了一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap