// // LogCommentVC.m // IBOSS // // Created by Dongke on 15/11/2. // Copyright © 2015年 elongtian. All rights reserved. // #import "LogCommentVC.h" #import "LogCommentTableViewCell.h" #import "LogShareAndCommentModel.h" #import "LogShareAndCommentInfoModel.h" #import "CommentDelegate.h" @interface LogCommentVC () @property(nonatomic,strong) ASIDownManager *mDownManager; @property(strong) IBOutlet 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 LogCommentVC -(instancetype)init{ self=[super init]; if(self){ self.filterArr=[NSMutableArray new]; self.searchArr=[NSMutableArray new]; self.model=[LogShareAndCommentModel new]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title=@"请选择"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageNamed:@"icon_back.png"] 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.tableView=[UITableView new]; self.tableView.dataSource=self; self.tableView.delegate=self; self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; self.tableView.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-44-20) ; [self.view addSubview:self.tableView]; //搜索框 self.searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; self.searchBar.delegate=self; self.tableView.tableHeaderView=self.searchBar; } /** 高度 @return <#return value description#> */ - (NSMutableDictionary *)heights{ if (_heights == nil){ _heights = [NSMutableDictionary dictionary]; } return _heights; } -(void)viewWillAppear:(BOOL)animated{ if(!_filterArr){ _filterArr=[NSMutableArray new]; _searchArr=[NSMutableArray new]; } [_searchArr addObjectsFromArray:_filterArr]; [_model setShareAndCommentArr:_filterArr ]; [self.tableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)dealloc{ [self cancel]; } #pragma mark - Table view data source cell -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"LogCommentTableViewCell"; LogCommentTableViewCell* cell=(LogCommentTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; cell=[[LogCommentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; LogShareAndCommentInfoModel* infoModel=self.model.shareCommentArr[indexPath.row]; infoModel.isChecked=NO; [cell parseInfoModel:infoModel]; cell.delegate=self; cell.cellIndex=indexPath.row; 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 89.0; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return [self.heights[@(indexPath.row)] doubleValue]; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self.commentDelegate reCommentData:self.model.shareCommentArr[indexPath.row]]; [self.navigationController popViewControllerAnimated:YES]; } #pragma 返回 -(void)goBack { [self.navigationController popViewControllerAnimated:YES]; } //进度条取消 - (void)cancel { [self stopLoading]; } -(void)addCancelButton { //添加取消按钮 UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom]; [button2 addTarget:self action:@selector(cancelSearch) forControlEvents:UIControlEventTouchUpInside]; button2.frame = CGRectMake(0, 0, 40, 24); [button2 setTitle:@"取消" forState:UIControlStateNormal]; [button2 setTitleColor:NavBarItemColor forState:UIControlStateNormal]; UIBarButtonItem *menuButton2 = [[UIBarButtonItem alloc] initWithCustomView:button2]; self.navigationItem.rightBarButtonItem = menuButton2; } -(void)cancelSearch { [_searchBar resignFirstResponder]; } -(void) removeCancel { self.navigationItem.rightBarButtonItem = nil; } #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 setShareAndCommentArr:_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 setShareAndCommentArr:_filterArr ]; [_tableView reloadData]; } - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; [self cancelSearch]; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [_searchBar resignFirstResponder]; [self cancelSearch]; } @end