| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- //
- // BrandSearchViewController.m
- // IBOSSmini
- //
- // Created by apple on 2017/5/23.
- // Copyright © 2017年 elongtian. All rights reserved.
- //
- #import "BrandSearchViewController.h"
- #import "BaseIDAndNameModel.h"
- @interface BrandSearchViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
- @property(nonatomic,strong) ASIDownManager *downManager;
- @property(nonatomic,strong) UISearchBar *searchBar;
- @property(nonatomic,strong) UITableView *tableView;
- @end
- @implementation BrandSearchViewController
- #pragma mark - 公共函数
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.navigationItem.title=@"品牌列表";
- //是否是dismissViewController
-
- if (_isPresentViewFlag) {
- UIView *vTitle = [[UIView alloc]init];
- vTitle.frame = CGRectMake(0, 0, Screen_Width, 44 + rectStatusHeight);
- vTitle.backgroundColor = NavigationBarTintColor;
- [self.view addSubview:vTitle];
-
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- [button setImage:[UIImage imageNamed:@"icon_back"]
- forState:UIControlStateNormal];
- [button addTarget:self action:@selector(goBack)
- forControlEvents:UIControlEventTouchUpInside];
- button.frame = CGRectMake(20, rectStatusHeight + 13, 9, 15);
- [vTitle addSubview:button];
- UILabel *lblTitle = [[UILabel alloc]init];
- lblTitle.frame = CGRectMake(9, rectStatusHeight + 13, Screen_Width-2*9 - 20, 15);
- lblTitle.textColor = [UIColor whiteColor];
- lblTitle.text = @"请选择";
- lblTitle.textAlignment = NSTextAlignmentCenter;
- [vTitle addSubview:lblTitle];
- }else{
- self.navigationItem.title=@"请选择";
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- [button setImage:[UIImage imageNamed:@"icon_back"]
- forState:UIControlStateNormal];
- [button addTarget:self action:@selector(goBack)
- forControlEvents:UIControlEventTouchUpInside];
- button.frame = CGRectMake(0, 0, 15, 18);
- UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
- self.navigationItem.leftBarButtonItem = menuButton;
- [self.navigationController setNavigationBarHidden:NO];
-
- }
-
- _tableView=[UITableView new];
- _tableView.delegate=self;
- _tableView.dataSource=self;
-
- //是否是dismissViewController
- if (_isPresentViewFlag) {
- self.tableView.frame = CGRectMake(0, 44 + rectStatusHeight, self.view.bounds.size.width, self.view.bounds.size.height - 44 - rectStatusHeight - 1) ;
- }
- else{
- self.tableView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-rectNavHeight - rectStatusHeight - 1) ;
- }
-
- [self.view addSubview:_tableView];
- //搜索框
- _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
- _searchBar.delegate=self;
- self.tableView.tableHeaderView=_searchBar;
- }
- /**
- viewWillAppear
-
- @param animated <#animated description#>
- */
- - (void)viewWillAppear:(BOOL)animated
- {
- _searchArr=[NSMutableArray new];
- _filterArr=[NSMutableArray new];
- _downManager = [[ASIDownManager alloc] init];
- _downManager.delegate = self;
- _downManager.OnImageDown = @selector(onLoadFinish:);
- _downManager.OnImageFail = @selector(onLoadFail:);
- [self reloadData];
-
- }
- /**
- dealloc
- */
- - (void)dealloc
- {
- [self cancel];
- }
- #pragma mark - 委托回调函数
- #pragma mark - tableView回调
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return _filterArr.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
- if(cell==nil)
- {
- cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- }
- NSDictionary *dic=_filterArr[indexPath.row];
- // cell.tag=[[dic objectForKey:@"BrandID"] intValue];
- cell.textLabel.text=[dic objectForKey:@"BrandName"];
- cell.textLabel.textAlignment = NSTextAlignmentCenter;
- cell.textLabel.font = [UIFont systemFontOfSize:TextFontOfSize];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- NSDictionary *dic=_filterArr[indexPath.row];
- BaseIDAndNameModel *model = [BaseIDAndNameModel new ];
- model.id = [dic objectForKey:@"BrandID"];
- model.name = [dic objectForKey:@"BrandName"];
- if ([self.bDelegate respondsToSelector:@selector(baseIDAndNameDoneDatas : BaseIDAndName:)]){
- [self.bDelegate baseIDAndNameDoneDatas:model BaseIDAndName:self.showDialogViewTag];
- }
- //是否是dismissViewController
- if (_isPresentViewFlag) {
- [self dismissViewControllerAnimated:YES completion:nil];
- }else{
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- #pragma mark - 数据回调
- /**
- 数据加载成功回调
-
- @param sender <#sender description#>
- */
- - (void)onLoadFinish:(ASIDownManager *)sender
- {
- // 服务器返回数据
- NSDictionary *dic = [sender.mWebStr JSONValue];
- [self cancel];
- // 服务器返回数据是否正确
- if (dic && [dic isKindOfClass:[NSDictionary class]])
- {
- // 服务器返回数据状态值
- int iStatus = [[dic objectForKey:@"Status"] intValue];
- // 服务器返回数据消息
- NSString *message=[dic objectForKey:@"Message"];
- // 服务器返回数据状态值正确
- if (iStatus == 0)
- {
- NSArray * approvArr=[dic objectForKey:@"Result"];
- if(approvArr!=nil)
- {
- _searchArr=[[NSMutableArray alloc]initWithArray:approvArr];
- _filterArr=[[NSMutableArray alloc] initWithArray:_searchArr];
- if(_searchArr.count==0)
- {
- [self showAlertViewText:@"未找到匹配结果"];
- }
- [self.tableView reloadData];
- }
- }
- // 服务器返回数据状态值异常
- else if(iStatus==ActionResultStatusAuthError
- ||iStatus==ActionResultStatusNoLogin
- ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){
- [self showReLoginDialog:message];
- }
- else {
- [self.tableView reloadData];
- [self showAlertViewText:message];
- }
-
- }
- }
- /**
- 加载失败
-
- @param sender <#sender description#>
- */
- - (void)onLoadFail:(ASIDownManager *)sender {
- [self cancel];
- [self.tableView reloadData];
- [self showAlertViewText:@"加载失败"];
- }
- #pragma mark searchbar回调
- - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
- {
- [self addCancelButton];
- return YES;
- }
- - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- {
- return YES;
- }
- - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
- {
-
- if ([searchText isEqualToString:@""]) {
- self.filterArr = _searchArr;
- [_tableView reloadData];
- return;
- }
-
- NSString *keyName = @"BrandName";
-
- //< 模糊查找
- NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", keyName, searchText];
- //< 精确查找
- // NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"%K == %@", keyName, searchText];
-
- NSLog(@"predicate %@",predicateString);
-
- NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[_searchArr filteredArrayUsingPredicate:predicateString]];
-
- self.filterArr = filteredArray;
- [_tableView reloadData];
- }
- - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
- {
- [searchBar resignFirstResponder];
- [self cancelSearch];
- }
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
- {
- [_searchBar resignFirstResponder];
- [self cancelSearch];
- }
- #pragma mark - 私有函数
- /**
- 进度条隐藏
- */
- - (void)cancel {
- [self stopLoading];
- }
- /**
- 添加取消按钮
- */
- - (void)addCancelButton
- {
- //添加取消按钮
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- [button addTarget:self action:@selector(cancelSearch)
- forControlEvents:UIControlEventTouchUpInside];
- button.frame = CGRectMake(0, 0, 40, 24);
- [button setTitle:@"取消" forState:UIControlStateNormal];
- [button setTitleColor:NavBarItemColor forState:UIControlStateNormal];
- UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
- self.navigationItem.rightBarButtonItem = menuButton;
- }
- /**
- 取消查询
- */
- - (void)cancelSearch
- {
- [_searchBar resignFirstResponder];
- [self removeCancel];
- }
- /**
- 取消按钮隐藏
- */
- - (void) removeCancel
- {
- self.navigationItem.rightBarButtonItem = nil;
- }
- /**
- 加载数据
- */
- - (void)reloadData
- {
- [self startLoading];
- NSString *urlStr = ServerURL;
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
- [dict setObject:@"GetDataSource" forKey:@"Action"];
- [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"];
- [dict setObject:kkUserCode forKey:@"UserCode"];
- [dict setObject:kkUserPwd forKey:@"UserPassword"];
- [dict setObject:kkSessionKey forKey:@"SessionKey"];
- [dict setObject:@"T_MST_Goods_Brand" forKey:@"DataSourceCode"];
- [_downManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];
- }
- /**
- 返回父界面
- */
- - (void)goBack
- {
- //是否是dismissViewController
- if (_isPresentViewFlag) {
- [self dismissViewControllerAnimated:YES completion:nil];
- }else{
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- @end
|