TableViewController中的搜索框和mj刷新配合使用

来源:互联网 发布:软件管理系统php 编辑:程序博客网 时间:2024/05/16 01:14

很早之前我写的又一篇博客,讲解IOS中UISearchController搜索框筛选功能实现,在此附上链接地址:http://blog.csdn.net/qq_29892943/article/details/48182275
然后最近在使用mj刷新的时候碰到了一个问题。
首先看代码:

#import "JYTableViewController.h"#import "MJRefresh.h"@interface JYTableViewController ()<UISearchResultsUpdating>@property(nonatomic,strong)NSMutableArray *datasourse;@property (nonatomic, strong) UISearchController *searchController;@property (strong,nonatomic) NSMutableArray  *searchList;@end@implementation JYTableViewController-(NSMutableArray *)datasourse{    if (_datasourse==nil) {        _datasourse=[NSMutableArray array];    }    return _datasourse;}- (void)viewDidLoad {    [super viewDidLoad];    self.navigationItem.title=@"商品列表";    [self addsearchvc];    [self addRefresh];}-(void)addRefresh{    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];    [self.tableView.mj_header beginRefreshing];}-(void)loadNewData{    [self addDatas];}-(void)addDatas{    for (int i=0; i<50; i++) {        [self.datasourse addObject:[NSString stringWithFormat:@"商品%d",i]];    }    [self.tableView reloadData];    [self.tableView.mj_header endRefreshing];}-(void)addsearchvc{    _searchController=[[UISearchController  alloc]initWithSearchResultsController:nil];    _searchController.searchBar.translucent=NO;//设置背景不透明    _searchController.searchBar.barTintColor=[UIColor grayColor];    //设置searchbar的边框颜色和背景颜色一致    _searchController.searchBar.layer.borderWidth=1;    _searchController.searchBar.layer.borderColor=[[UIColor grayColor] CGColor];    _searchController.searchBar.placeholder=@"查询商品";    _searchController.searchResultsUpdater = self;    _searchController.dimsBackgroundDuringPresentation = NO;    _searchController.hidesNavigationBarDuringPresentation = NO;    _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);    self.tableView.tableHeaderView = self.searchController.searchBar;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    if (self.searchController.active) {        return [self.searchList count];    }else{        return [self.datasourse count];    }}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    NSString *dentifier=@"cellid";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dentifier];    if (cell==nil) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:dentifier];    }    if (self.searchController.active) {        [cell.textLabel setText:self.searchList[indexPath.row]];    }    else{        [cell.textLabel setText:self.datasourse[indexPath.row]];    }    return cell;}-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {    if (self.searchController.active) {        self.tableView.mj_header.hidden=YES;    }else{        self.tableView.mj_header.hidden=NO;    }    NSString *searchString = [self.searchController.searchBar text];    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];    if (self.searchList!= nil) {        [self.searchList removeAllObjects];    }    //过滤数据    self.searchList= [NSMutableArray arrayWithArray:[_datasourse filteredArrayUsingPredicate:preicate]];    //刷新表格    [self.tableView reloadData];}@end

运行如下:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

在这里多说一句,如果你点击搜索时,出现的不是中文“取消”两个字,而是“search”的话,那么可能你的项目语言没有设置,设置很简单,在此我也附上一张图。

这里写图片描述

由于我不会做gif动态,所以上述四张图片你发现问题了吗?对,就是第三张,下拉的时候发现不显示刷新控件,松开之后上移的时候才展示mj刷新控件。这个问题也让我找了半天,发现试了好多情况都不行。所以UISearchController也有坑啊。

如何解决呢?我找了一些网上资料,也不好实现,所以干脆我们来自己做一个UISearchController,模仿他的功能不就行了?

直接上代码喽!

#import "JYTableViewController.h"#import "MJRefresh.h"@interface JYTableViewController ()<UISearchBarDelegate,UISearchBarDelegate>@property(nonatomic,strong)NSMutableArray *datasourse;@property (strong,nonatomic) NSMutableArray  *searchList;@property (assign,nonatomic) BOOL active;@property(nonatomic,weak)UISearchBar *searchbar;@end@implementation JYTableViewController-(NSMutableArray *)datasourse{    if (_datasourse==nil) {        _datasourse=[NSMutableArray array];    }    return _datasourse;}- (void)viewDidLoad {    [super viewDidLoad];    self.navigationItem.title=@"商品列表";    [self addsearchvc];    [self addRefresh];}-(void)addRefresh{    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];    [self.tableView.mj_header beginRefreshing];}-(void)loadNewData{    [self addDatas];}-(void)addDatas{    for (int i=0; i<50; i++) {        [self.datasourse addObject:[NSString stringWithFormat:@"商品%d",i]];    }    [self.tableView reloadData];    [self.tableView.mj_header endRefreshing];}-(void)addsearchvc{    UISearchBar *searchbar=[[UISearchBar alloc]init];    self.searchbar=searchbar;    searchbar.delegate=self;    searchbar.placeholder=@"搜索设备";    searchbar.frame=CGRectMake(0, 0, self.view.frame.size.width, 44);    [searchbar setBackgroundImage:[UIImage imageNamed:@"searchbackground.png"]];    self.tableView.tableHeaderView=searchbar;    [searchbar setReturnKeyType:UIReturnKeyDone];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    if (self.active) {        return [self.searchList count];    }else{        return [self.datasourse count];    }}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    NSString *dentifier=@"cellid";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dentifier];    if (cell==nil) {        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:dentifier];    }    if (self.active) {        [cell.textLabel setText:self.searchList[indexPath.row]];    }    else{        [cell.textLabel setText:self.datasourse[indexPath.row]];    }    return cell;}//点击了取消按钮-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{    [self.searchbar resignFirstResponder];    [self.searchbar setShowsCancelButton:NO animated:YES];    self.searchbar.text=@"";    self.active=NO;    [self searchBar:searchBar textDidChange:@""];}//点击了搜索按钮-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{    [self.searchbar resignFirstResponder];    [self.searchbar setShowsCancelButton:NO animated:YES];    self.searchbar.text=@"";    self.active=NO;    [self searchBar:searchBar textDidChange:@""];}-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{    self.active=YES;    [self searchBar:searchBar textDidChange:@""];    [self.searchbar setShowsCancelButton:YES animated:YES];    for (id obj in [searchBar subviews]) {        if ([obj isKindOfClass:[UIView class]]) {            for (id obj2 in [obj subviews]) {                if ([obj2 isKindOfClass:[UIButton class]]) {                    UIButton *btn = (UIButton *)obj2;                    [btn setTitle:@"取消" forState:UIControlStateNormal];                }            }        }    }    return YES;}- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{    if(self.active){        self.tableView.mj_header.hidden=YES;    }else{        self.tableView.mj_header.hidden=NO;    }    NSString *searchString = searchText;    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];    if (self.searchList!= nil) {        [self.searchList removeAllObjects];    }    //过滤数据    self.searchList= [NSMutableArray arrayWithArray:[_datasourse filteredArrayUsingPredicate:preicate]];    //刷新表格    [self.tableView reloadData];}@end

运行效果如下:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

现在,这个功能用起来,和UISearchController一样喽!

1 0
原创粉丝点击