我创建了一个简单的tableView并设置了一个searchController。我需要我的tableview y点以70px开头。当我触摸searchBar时,它没有在屏幕顶部显示打开的栏。相反,它从70px略微上升。
我不熟悉swift或storyboard。我想使用客观的c语言。
方法:viewDidLoad中
cateArray_ = [ [ NSArray alloc ] initWithObjects:@"Apple", @"Fruits", @"Vegetables", @"1", @"350", @"Shell", @"Stocks", @"Numbers", nil ];
self.tableView_ = [ [ UITableView alloc ] initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height - 70 ) style:UITableViewStylePlain ];
self.tableView_.delegate = self;
self.tableView_.dataSource = self;
self.tableView_.bounces = YES;
self.tableView_.alwaysBounceVertical = YES;
UIView* footerToRemoveBorderLine = [ [ UIView alloc ] initWithFrame:CGRectZero ];
self.tableView_.tableFooterView = footerToRemoveBorderLine;
self.tableView_.backgroundColor = [ UIColor colorWithRed:1.0 green:0.95 blue:0.95 alpha:1.0 ];
self.tableView_.contentOffset = CGPointMake(0, 44);
[ self.view addSubview:self.tableView_ ];
[ self setTitle:@"Category" ];
//Search controller
self.searchCtrl_ = [ [ UISearchController alloc ] initWithSearchResultsController:nil ];
[ self.searchCtrl_ setDimsBackgroundDuringPresentation:false ];
[ self.searchCtrl_.searchBar sizeToFit ];
[ self.searchCtrl_ setSearchResultsUpdater:self ];
[ self.searchCtrl_.searchBar setBarStyle:UIBarStyleDefault ];
[ self.searchCtrl_.searchBar setAutocorrectionType:UITextAutocorrectionTypeNo ];
[ self.searchCtrl_.searchBar setDelegate:self ];
[ self.searchCtrl_.searchBar setScopeButtonTitles:[ NSArray arrayWithObjects:@"All", @"Alpha", @"Numbers", nil ] ];
self.tableView_.tableHeaderView = self.searchCtrl_.searchBar;
方法搜索代理
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
[ self updateSearchResultsForSearchController:self.searchCtrl_ ];
}
// Called when the search bar becomes first responder
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
// Set searchString equal to what's typed into the searchbar
NSString* searchString = [ self.searchCtrl_.searchBar text ];
NSString* scope = [ [ self.searchCtrl_.searchBar scopeButtonTitles ] objectAtIndex:[ self.searchCtrl_.searchBar selectedScopeButtonIndex ] ];
[ self filterContentForSearchText:searchString Scope:scope ];
}
当我通过点击打开searchBar时,它会在打开时稍微移动搜索的灰色部分。但我希望它位于状态栏的顶部或下方。
有人可以帮助我解决我做错了什么或者有什么工作吗?
我需要tableView的table y点从70开始,因为上面的表视图我将保留一些范围栏和文本。
原谅我的英语不好,非常感谢你的帮助。
答案 0 :(得分:1)
//try the below code
#import "ViewController.h"
@interface ViewController ()
{
UITableView*tableView_;
UISearchController*searchCtrl_;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableView_ = [ [ UITableView alloc ] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain ];
//tableView_.delegate = self;
//tableView_.dataSource = self;
tableView_.bounces = YES;
tableView_.alwaysBounceVertical = YES;
UIView* footerToRemoveBorderLine = [ [ UIView alloc ] initWithFrame:CGRectZero ];
tableView_.tableFooterView = footerToRemoveBorderLine;
tableView_.backgroundColor = [ UIColor colorWithRed:1.0 green:0.95 blue:0.95 alpha:1.0 ];
tableView_.contentOffset = CGPointMake(0, 0);
[ self.view addSubview:tableView_ ];
[ self setTitle:@"Category" ];
//Search controller
searchCtrl_ = [ [ UISearchController alloc ] initWithSearchResultsController:nil ];
[ searchCtrl_ setDimsBackgroundDuringPresentation:false ];
[ searchCtrl_.searchBar sizeToFit ];
[ searchCtrl_ setSearchResultsUpdater:self ];
[ searchCtrl_.searchBar setBarStyle:UIBarStyleDefault ];
[ searchCtrl_.searchBar setAutocorrectionType:UITextAutocorrectionTypeNo ];
[ searchCtrl_.searchBar setDelegate:self ];
[ searchCtrl_.searchBar setScopeButtonTitles:[ NSArray arrayWithObjects:@"All", @"Alpha", @"Numbers", nil ] ];
tableView_.tableHeaderView = searchCtrl_.searchBar;
}