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
- TableViewController中的搜索框和mj刷新配合使用
- MJ刷新
- mj使用
- 用MJ 刷新加载
- MJ下拉刷新
- MJ刷新总结
- C# 中的用户自定义控件和WPF的配合使用
- CloneCD和IsoBuster配合使用以提取VCD中的文件
- 百分比和绝对值在redis中的配合使用
- 如何在使用storyboard中使用tableViewController中的Prototype Cells 自定义cell
- 使用PopupWindow配合EditText制作模糊搜索
- iOS PAD编写 POPOver 使用tableviewcontroller的两种操作方式,代理和消息通知
- TortoiseCVS和CVSNT配合使用
- TortoiseCVS和CVSNT配合使用
- Makefile和shell配合使用
- QFile和QTextStream配合使用
- NSURLSession和AFNetWorking配合使用
- Service和BroadCast配合使用
- JZ2440 启动NFS网络文件系统_初试led驱动
- iTextSharp 绘制字体中特殊符号到PDF
- CUDA编程(一):背景与安装
- 图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()
- AsyncTask异步任务类使用
- TableViewController中的搜索框和mj刷新配合使用
- picasso-强大的Android图片下载缓存库
- 善用iOS App中Web View控件的好处
- Android Studio 中api>9 之后无法找到 org.apache.http 包的问题
- android设计模式之装饰模式(一)
- 安卓蓝牙BLE设备开发
- 理解jquery的$.extend()、$.fn和$.fn.extend()
- HttpClient抓包返回值乱码
- Linux strace命令