UItableview 添加 uisearchController

@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];
    }
时间: 2024-10-31 11:23:29

UItableview 添加 uisearchController的相关文章

[iOS]swift之UITableView添加通过xib创建的headerView坑爹问题

情景是这样的,我UITableView添加了一个HeaderView,这个HeaderView是通过xib创建,是UIView.出来的结果却出乎意料,UITableView的Cell最顶部的几个被HeaderView给遮挡了---我勒个去--神马情况???!!! 于是哥通过看层次结构,发现运行出来的HeaderView和Cell列表不在同一层次,理应是同一层才对呀!!!!于是我用其他xib试试,情况一样,然后改用代码创建 UIView() 类似这种方式,这样就是正常的,HeaderView和Ce

#iOS开发常用方法集锦#为UITableView添加UISwipeGestureRecognizer手势

? 本文永久地址为http://www.cnblogs.com/ChenYilong/p/4103039.html ,转载请注明出处. 印象笔记链接:https://app.yinxiang.com/shard/s22/sh/04150175-aac6-4981-b71d-d7246de3037b/a0f139b2619a4607 ? ? ? <UIGestureRecognizerDelegate> -(void)viewDidLoad { ? ? [superviewDidLoad]; ?

UITableView添加波浪动画效果

- (void)reloadDataAnimateWithWave:(WaveAnimation)animation; { [self setContentOffset:self.contentOffset animated:NO]; [UIView animateWithDuration:.2 animations:^{ [self setHidden:YES]; [self reloadData]; } completion:^(BOOL finished) { //Do something

UITableView与UISearchController搜索及上拉加载,下拉刷新

1 #import "ViewController.h" 2 #import "TuanGouModel.h" 3 #import "TuanGouTableViewCell.h" 4 #define kDeviceWidth [UIScreen mainScreen].bounds.size.width 5 #define kDeviceHeight [UIScreen mainScreen].bounds.size.height 6 @int

UITableView添加UITapGestureRecognizer与didSelectRowAtIndexPath冲突解决方法

在UITableView上添加了UITapGestureRecognizer后会导致didSelectRowAtIndexPath失效,原因是UITapGestureRecognizer会截取了tableView的touch事件,导致无法响应行选择,解决方法是重写UIGestureRecognizerDelegate中的 1 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(

UIView添加手势 然后UITableView 添加进这个View 导致UITableView 的单元格点击事件无效

#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIView * v = [[UIView alloc] init

Swift - 给表格UITableView添加索引功能(快速定位)

像iOS中的通讯录,通过点击联系人表格右侧的字母索引,我们可以快速定位到以该字母为首字母的联系人分组. 要实现索引,我们只需要两步操作: (1)实现索引数据源代理方法 (2)响应点击索引触发的代理事件 效果图如下: 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 4

UIView UITableView 背景图片添加

这几天,经常用到为某个视图设置背景图片,而API中UIView没有设置背景图片的方法,搜集归纳如下: 第一种方法: 利用的UIView的设置背景颜色方法,用图片做图案颜色,然后传给背景颜色. UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"bgImg.png"]; UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,3

iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)

在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISearchDisplayController的组合方式. 添加UISearchController属性: @property(strong, nonatomic) UISearchController *searchController; @property(strong, nonatomic) NS