首页 > 其他 > 详细

UItableview 添加 uisearchController

时间:2016-05-02 12:56:27      阅读:112      评论:0      收藏:0      [点我收藏+]
@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];
    }

 

UItableview 添加 uisearchController

原文:http://www.cnblogs.com/frounk/p/5452110.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!