【发布时间】:2016-05-21 08:38:38
【问题描述】:
我一直想知道如何针对这种情况实施最佳实践。
我有一个 UITableViewController,并且想以编程方式添加 searchBar,所以我在 viewForHeaderInSection 上这样做了
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
searchBar.placeholder = @"Search";
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchBar.delegate=self;
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self; //This one keep messing with the layout
[view setBackgroundColor:[UIColor blackColor]];
[view addSubview:searchBar];
return view;
}
如下图所示,在编辑时导致我的 searchBar 位置和行为怪异的注释行。
第一响应者使状态栏重叠,虽然我已经添加了我的 viewDidLoad
self.edgesForExtendedLayout = UIRectEdgeNone;
如果我删除了 searchDisplayController.searchResultsDelegate = self;所有的视图看起来都很完美,但我需要这个委托来触发 searchResultTableView 上的 didSelectedRow。这怎么可能?这种情况的最佳方法是什么。谢谢!
更新
由于我在这里和那里搜索,并且在给出的答案的帮助下,我可以得出结论,如果你想在 UITableView 上修复 SearchBar,只需在 UIViewController 上创建 tableview,放置 searchBar,放置一些约束,然后瞧!
【问题讨论】:
-
我提供了一个答案,但我刚刚意识到您是在使用 Objective-C 而不是 Swift 进行编码。我很抱歉。不过,转换为objective-c应该很容易。这是完全相同的过程,只是语法略有不同。
-
我在我的答案中添加了一个链接,它将向您展示如何在 Objective-c 中执行此操作。祝你好运!
标签: ios objective-c uitableview uisearchbar uisearchdisplaycontroller