在UISearchDisplayController中添加UISegmentedControl
我想在searchBar
下面和UISearchDisplayController
的TableView上面添加UISegmentedControl
。 目前, UISearchDisplayController
仅在其SearchBar
下显示其tableView
。
但我想在SearchBar
下面添加一个UISegmentedControl
,以便我在SearchBar
上面有SearchBar
,在SearchBar
之后我有一个UISegmentedControl
,在UISegmentedControl
下面我有UITableView
。 这是使用UISearchDisplayController
做到这一点的任何方式,或者我必须创建自己的SearchDisplayController
,它有自己的SearchBar
, UISegmentedControl
和TableView
?
有什么建议吗? 谢谢
通过添加scop标题启用ShowScopeBar
并添加尽可能多的段,并通过以下方法处理它们
按代码
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; [searchBar setShowsScopeBar:YES]; [searchBar setScopeButtonTitles:@[@"button1",@"button2"]]; [[self view] addSubview:searchBar];
通过XIB
委托方法来处理范围按钮操作
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{ }
在表格中创建一个视图
UIView *viewsearch=[[UIView alloc]initWithFrame:CGRectMake(0,-10, 320,83)]; [self.tblname addSubview:viewsearch]; [self.view addGestureRecognizer:revealController.panGestureRecognizer] UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,5, 320, 40)]; searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; searchBar.delegate=self; [searchBar setBarTintColor:[UIColor colorWithRed:(249/255.0) green:(9/255.0) blue:(99/255.0) alpha:1]]; searchBar.tintColor = [UIColor whiteColor]; searchBar.placeholder = @"Search items eg jacket";
在该视图中添加搜索栏视图。
[viewsearch addSubview:searchBar]; searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
在此searchview中使用UISegmentedControl。
NSArray *itemArray = [NSArray arrayWithObjects: @"General", @"Near me", nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]; segmentedControl.frame = CGRectMake(50,53, 225, 22); segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; [segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged]; segmentedControl.selectedSegmentIndex = 0; segmentedControl.tintColor = [UIColor colorWithRed:(249/255.0) green:(10/255.0) blue:(99/255.0) alpha:1]; segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin; [viewsearch addSubview:segmentedControl];
我找到了我的问题的解决方案,我添加了UISegmentedControl作为UISearchDisplayController的TableView的头。 这样看起来我刚刚在UISearchDisplayController的searchBar下添加了一个UISegmentedControl。
谢谢每一个试图帮助的人。