需求:实现根据搜索内容 展示在TableView上,搜索之前或者搜索文本为空则直接展示接口数据
(UITextfield/UITableView)
pod'NSPredicateSearch', '~> 0.1.2'
ViewController.m中:
#import "NSPredicateSearch/NSPredicate+Search.h"
@property (nonatomic, strong)NSMutableArray *mulArray; @property (nonatomic, strong)NSMutableArray *temArray;
自定义UITextfield初始化包括:
```
1: [self.header.searchTf addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
```
3:事件方法 和其中的优化处理
- (void)textFieldDidChanged:(UITextField *)textField {//此判断不能放在textfield的shouldChangeCharactersInRange协议方法中 UITextRange *markedTextRange = textField.markedTextRange; //如果存在待选文字记录,则暂不处理 markedTextRange == nil说明不存在待选文字 if (markedTextRange == nil) { [self selectBankList]; }}
-(void)selectBankList{ //刷新表格 if (self.header.searchTf.text.length == 0) { self.mulArray =self.temArray; [self.searchTabView reloadData]; return; } // 重要 NSPredicate *predicate = [NSPredicate predicateWithSearch:self.header.searchTf.text searchTerm:@"searchTerms"]; self.mulArray = [[NSMutableArray alloc] initWithArray:[self.temArray filteredArrayUsingPredicate:predicate]]; [self.searchTabView reloadData];}
接口:↓↓↓↓↓↓↓↓↓
SearchTypeModel.h
// 筛选标识 (根据这个标识,可直接对下面两个属性做过滤搜索 @property (strong, nonatomic) NSString* searchTerms;
@property (nonatomic, copy) NSString *ExchangeCode; @property (nonatomic, copy) NSString *ExchangeName;
SearchTypeModel.m (NSString *)searchTerms { return [NSString stringWithFormat:@"%@ %@ %@",self.ExchangeCode, self.ExchangeName];}
通过接口获取字典数组数据,以Model取;接口数据:数组字典参考
(lldb) po response
{
code = 0;
data = (
{
ExchangeCode = APEX;
ExchangeName = "Asia Pacific Exchange";
OmnibusSupport = 0;
},
{
ExchangeCode = BMD;
ExchangeName = "Bursa Malaysia Derivatives Berhad";
OmnibusSupport = 0;
},
{
ExchangeCode = IFUS;
ExchangeName = "ICE Futures U.S.";
OmnibusSupport = 0;
},
{
ExchangeCode = INE;
ExchangeName = "Shanghai International Energy Exchange";
OmnibusSupport = 0;
},
{
ExchangeCode = NFX;
ExchangeName = "Nasdaq Futures, Inc";
OmnibusSupport = 0;
},
{
ExchangeCode = NYMEX;
ExchangeName = "New York Mercantile Exchange";
OmnibusSupport = 0;
},
{
ExchangeCode = OSE;
ExchangeName = "Osaka Stock Exchange";
OmnibusSupport = 0;
},
{
ExchangeCode = SGX;
ExchangeName = "Singapore Exchange";
OmnibusSupport = 0;
},
{
ExchangeCode = TFEX;
ExchangeName = "Thailand Futures Exchange";
OmnibusSupport = 0;
},
{
ExchangeCode = TOCOM;
ExchangeName = "Tokyo Commodity Exchange";
OmnibusSupport = 0;
}
);
msg = success;
}