UISearchBar、UISearchDisplayController搜索

来源:互联网 发布:贪心算法最小生成树 编辑:程序博客网 时间:2024/05/05 04:11

UISearchBar * search=[[UISearchBaralloc] initWithFrame:CGRectMake(0,0, 320,44)];

    search.showsCancelButton=YES;//显示取消按钮

    search.autocapitalizationType=UITextAutocorrectionTypeNo;//大小写

    self.searchBar=search;

    self.tableView.tableHeaderView=search;//搜索栏显示在表格顶端

    

    

    UISearchDisplayController * display=[[UISearchDisplayController alloc]initWithSearchBar:search contentsController:self];

    display.searchResultsDataSource=self;

    display.searchResultsDelegate=self;

    //[display release];

    

    [search release];




--------------------------------------------------------




//输入的搜索内容

        NSPredicate * predicate=[NSPredicatepredicateWithFormat:@"SELF contains[cd] %@",self.searchBar.text];//cd不区分大小写   自身是否包含搜索栏的内容

        self.filterArr=[self.colors.allKeyssortedArrayUsingSelector:@selector(compare:)];

        self.filterArr=[self.filterArrfilteredArrayUsingPredicate:predicate];//筛选后的数组