UItableview 添加 uisearchController
2016-05-02 11:49
483 查看
@property (nonatomic, strong) UISearchController* searchController; self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.searchController.delegate=self; self.searchController.searchResultsUpdater = self; self.searchController.dimsBackgroundDuringPresentation = NO; self.searchController.hidesNavigationBarDuringPresentation = NO; // self.searchController.searchBar.backgroundColor=[UIColor blueColor]; //frame=CGRectMake(0, 0, ScreenWidth, 44); self.searchController.searchBar.layer.borderColor=[[UIColor colorWithHex:0xEEEEEE] CGColor]; self.searchController.searchBar.layer.borderWidth=0.5; self.searchController.searchBar.barTintColor=[UIColor colorWithHex:0xEEEEEE]; [self.searchController.searchBar sizeToFit]; self.tableView.tableHeaderView=self.searchController.searchBar; - (void)updateSearchResultsForSearchController:(UISearchController *)searchController { NSString *searchString = self.searchController.searchBar.text; [self.peopleSearchData removeAllObjects]; // UserModel *model = self.peopleData[indexPath.section][indexPath.row]; for (NSArray *modelarray in self.peopleData) { for (UserModel *model in modelarray) { NSRange range1 = [model.nickName rangeOfString:searchString]; NSRange range2 = [model.company rangeOfString:searchString]; if ((range2.length != 0||range1.length != 0)&&![model.name isEqualToString:@"NO_Section=0"]) { //range1.location != NSNotFound [self.peopleSearchData addObject:model]; } } // BOOL range = [model.name hasPrefix:searchString]; } if (_peopleSearchData.count!=0) { _tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine; }else{ _tableView.separatorStyle=UITableViewCellSeparatorStyleNone; } [self.tableView reloadData]; } - (void)willPresentSearchController:(UISearchController *)searchController{ } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView; { [self.searchController.searchBar resignFirstResponder]; NSLog(@"scrollViewWillBeginDragging"); } - (void)didPresentSearchController:(UISearchController *)searchController{ UIButton *cancelButton = nil; UIView *topView = self.searchController.searchBar.subviews[0]; for (UIView *subView in topView.subviews) { if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) { cancelButton = (UIButton*)subView; } } if (cancelButton) { //Set the new title of the cancel button // [cancelButton setTitle:@"取消" forState:UIControlStateNormal]; [cancelButton setTitleColor:[UIColor colorWithHex:0x31425E] forState:UIControlStateNormal]; cancelButton.titleLabel.font = [UIFont fontWithName:@"Heiti SC" size:15]; } } // 搜索框开始编辑时触发方法 - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { [self presentViewController:self.searchController animated:YES completion:nil]; }
if (self.searchController.active) { self.searchController.active = NO; [self.searchController.searchBar removeFromSuperview]; }
相关文章推荐
- [leetcode] 347. Top K Frequent Elements 解题报告
- Storyboard实现界面跳转
- Unique Paths II
- 纯java代码设置简单UI界面
- CoreThink主题开发(六)使用H-ui开发博客主题之首页友情链接标签混排
- UITableView基本介绍(一)
- Android_UI
- UIGestureRecognizer 手势浅析
- Unique Paths
- 1369 - Answering Queries<数>
- PS2游戏摇杆joystick与arduino uno简单实验
- XMG UITabBarController
- CoreThink主题开发(五)使用H-ui开发博客主题之首页显示站点公告
- UIAlertView和UIAlertController的使用
- C#OOP之十五 String类&StringBuilder类
- C#OOP之十五 String类&StringBuilder类
- C#OOP之十五 String类&StringBuilder类
- UIActivityIndicatorView官方文档及使用
- UIPageControl
- [Leetcode刷题总结系列][Dynamic Programming]62. Unique Paths