您的位置:首页 > 产品设计 > UI/UE

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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: