// // LogShareVC.m // IBOSS // // Created by Dongke on 15/11/4. // Copyright © 2015年 elongtian. All rights reserved. // #import "LogShareVC.h" #import "LogShareTableViewCell.h" #import "LogShareAndCommentModel.h" #import "LogShareAndCommentInfoModel.h" @interface LogShareVC () @property(nonatomic,strong) ASIDownManager *mDownManager; @property(strong) UISearchBar *searchBar; @property(strong) UITableView *tableView; @property (nonatomic,assign)DataSearchState* searchState; @property(nonatomic,strong) LogShareAndCommentModel *model; @property(nonatomic,strong) UIBarButtonItem *menuButtonOk; @property(nonatomic,strong) UIButton *btnOk; @end @implementation LogShareVC - (void)viewDidLoad { [super viewDidLoad]; self.filterArr=[[NSMutableArray alloc]init]; self.searchArr=[[NSMutableArray alloc]init]; self.model=[LogShareAndCommentModel new]; [self.navigationController setNavigationBarHidden:NO]; if (_isPresentViewFlg) { UIView *vTitle = [[UIView alloc]init]; vTitle.frame = CGRectMake(0, 0, Screen_Width, 44 + rectStatusHeight); vTitle.backgroundColor = [UIColor colorWithRed:189.0/255.0 green:0 blue:7.0/255.0 alpha:1]; [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]; _btnOk = [UIButton buttonWithType:UIButtonTypeCustom]; _btnOk.frame = CGRectMake(vTitle.frame.size.width-64, rectStatusHeight + 13,44, 20); [_btnOk addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside]; [_btnOk setTitle:@"确定" forState:UIControlStateNormal]; [_btnOk setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _btnOk.enabled=NO; [vTitle addSubview:_btnOk]; } else{ self.navigationItem.title=@"请选择"; self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:16]}; self.navigationController.navigationBar.barTintColor = NavigationBarTintColor; 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; //保存 _btnOk = [UIButton buttonWithType:UIButtonTypeCustom]; [_btnOk addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside]; _btnOk.frame = CGRectMake(0, 0, 44, 20); [_btnOk setTitle:@"确定" forState:UIControlStateNormal]; [_btnOk setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _btnOk.enabled=NO; _menuButtonOk = [[UIBarButtonItem alloc] initWithCustomView:_btnOk]; self.navigationItem.rightBarButtonItem = _menuButtonOk; } self.tableView=[UITableView new]; self.tableView.dataSource=self; self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; self.tableView.delegate=self; //是否是dismissViewController if (_isPresentViewFlg) { 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:self.tableView]; //搜索框 self.searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; self.searchBar.delegate=self; self.tableView.tableHeaderView=self.searchBar; if(_model){ if(![_model isSelectAtLeastOne]) { _btnOk.enabled=NO; } else { _btnOk.enabled=YES; } } self.mDownManager=[[ASIDownManager alloc] init]; self.mDownManager.delegate=self; self.mDownManager.OnImageDown=@selector(onLoadFinish:); self.mDownManager.OnImageFail=@selector(onLoadFail:); [self reloadData]; } -(void)viewWillAppear:(BOOL)animated{ } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)dealloc{ [self cancel]; } /** 高度 @return <#return value description#> */ - (NSMutableDictionary *)heights{ if (_heights == nil){ _heights = [NSMutableDictionary dictionary]; } return _heights; } #pragma mark - Table view data source cell -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"LogShareTableViewCell"; LogShareTableViewCell* cell=(LogShareTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell=[[LogShareTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; LogShareAndCommentInfoModel* infoModel=self.model.shareCommentArr[indexPath.row]; [cell parseInfoModel:infoModel]; cell.delegate=self; cell.cellIndex= (int)indexPath.row; BOOL ischeck=infoModel.isChecked; [cell setCheckBackground:ischeck]; self.heights[@(indexPath.row)]=@(cell.height); return cell; } #pragma mark -Table view data source number -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _filterArr.count; } - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 200.0; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return [self.heights[@(indexPath.row)] doubleValue]; } #pragma mark 保存完成 - (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 removeAllObjects]; [_searchArr addObjectsFromArray:approvArr]; [_filterArr removeAllObjects]; [_filterArr addObjectsFromArray:_searchArr]; if(_searchArr.count==0) { [self showAlertViewText:@"未找到匹配结果"]; } [_model parseShareAndCommentArr:_filterArr ]; [self.tableView reloadData]; } } else if(iStatus==ActionResultStatusAuthError ||iStatus==ActionResultStatusNoLogin ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){ [self showReLoginDialog:message]; return; } else { [self.tableView reloadData]; [self showAlertViewText:message]; } } } #pragma mark 保存失败 - (void)onLoadFail:(ASIDownManager *)sender { [self cancel]; [self.tableView reloadData]; [self showAlertViewText:@"加载失败"]; } #pragma mark 保存完成后 - (void)cancel { [self stopLoading]; } #pragma mark 加载数据 -(void)reloadData { [self startLoading]; NSString *urlStr = ServerURL; NSMutableDictionary *dict=[NSMutableDictionary new]; [dict setObject:@"GetDailyRecordReplyAndComment" forKey:@"Action"]; [dict setObject:kkAccountCode forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil]; } #pragma mark 返回 -(void)goBack { if (_isPresentViewFlg) { [self dismissViewControllerAnimated:YES completion:nil]; }else{ [self.navigationController popViewControllerAnimated:YES]; } } #pragma mark 保存 -(void)saveData{ if(![_model isSelectAtLeastOne]) { [self showAlertViewText:@"请选择分享人范围"]; return; } if([_tag isEqualToString:@"logAdd"]){ if([self.shareDelegate respondsToSelector:@selector(reShareData:)]){ [self.shareDelegate reShareData:self.model.checkedArr]; } } else if([_tag isEqualToString:@"logList"]){ if ([self.shareDelegate respondsToSelector:@selector(selectListItem:)]) { [self.shareDelegate selectListItem:[self.model checkedArr]]; } [self.navigationController setNavigationBarHidden:YES]; } //是否是dismissViewController if (_isPresentViewFlg) { [self dismissViewControllerAnimated:YES completion:nil]; }else{ [self.navigationController popViewControllerAnimated:YES]; } } #pragma mark cell 回調方法 -(void) btnCheckPressed:(LogShareTableViewCell *)cell { BOOL checked = [self.model updateCellChecked:cell.userId]; [cell setCheckBackground:checked]; if(![_model isSelectAtLeastOne]) { _btnOk.enabled=NO; } else { _btnOk.enabled=YES; } } #pragma mark search bar delegate - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { 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; [_model parseShareAndCommentArr:_filterArr ]; [_tableView reloadData]; return; } NSString *keyName = @"UserName"; //< 模糊查找 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; [_model parseShareAndCommentArr:_filterArr ]; [_tableView reloadData]; } @end