搜索栏有内容搜索商品,清空UITextField返回搜索历史
2016-02-23 12:42
411 查看
实现效果:点击主页的textfiled,进入到搜索页面,显示搜索历史记录以及热门推荐。再点击textfiled成为第一响应者,调出键盘。清空内容或者是滑动tableView取消textfiled的第一响应者,收起键盘。清空内容的时候加载头视图为nil,点击搜索的时候加载排序方法的头视图。
@interface searchViewController ()<UITableViewDataSource,UITableViewDelegate,ProductTableHeaderDelegate,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource> { NSUInteger _pageIndex; ProductListHeaderView *_headerView; int _cat_id; TableShowType _tabShowType; BOOL _asc; BOOL _search; BTextField * placeHolderLabel; } @end @implementation searchViewController -(void)loadData { } -(void)loadDataSearch { _pageIndex++; [[ApiManager sharedInstance] getProductList:_pageIndex searchString:[self getSearchString] pn_s:5 sort:[self getSortString] sort_v:[self getSort_VString] filter:nil success:^(id JSON){ if (_pageIndex == 1) { [_proudctListArray removeAllObjects]; } NSArray *goodsArray = JSON[@"goods"]; for (NSDictionary *goodsDict in goodsArray) { GoodsModel *goodsModel = [[GoodsModel alloc] initWithDictionary:goodsDict error:nil]; [_proudctListArray addObject:goodsModel]; } if (goodsArray.count <5) { [_productTableView.mj_footer endRefreshingWithNoMoreData]; }else{ [_productTableView.mj_footer endRefreshing]; } [_productTableView reloadData]; } failure:^(BNetworkError *error,id JSON){ }]; } -(void)loadView { [super loadView]; _search = NO; [self createUI]; } -(void)createUI{ [super createUI]; WS(ws) UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchButtonClick)]; rightBtnItem.tintColor = [UIColor blackColor]; self.navigationItem.rightBarButtonItem = rightBtnItem; [self addBackNav]; //搜索框 UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(20, 9, 434/2, 24)]; searchView.backgroundColor = [UIColor whiteColor]; searchView.layer.masksToBounds = YES; searchView.layer.cornerRadius = 12; placeHolderLabel = [[BTextField alloc] init]; placeHolderLabel.font = GetFont(14.0f); placeHolderLabel.placeholder =BoLocalizedString(@"cat_search_holder"); placeHolderLabel.textColor = BCOLOR_73; [searchView addSubview:placeHolderLabel]; [placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make){ make.edges.equalTo(searchView).with.insets(UIEdgeInsetsMake(0, 2, 0, 2)); }]; // placeHolderLabel.clearsOnBeginEditing = YES; placeHolderLabel.clearButtonMode =UITextFieldViewModeWhileEditing;// 编辑是显示清空按钮 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doaction) name:UITextFieldTextDidChangeNotification object:placeHolderLabel]; self.navigationItem.titleView = searchView; _productTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _productTableView.delegate = self; _productTableView.showsVerticalScrollIndicator = NO; _productTableView.separatorStyle = UITableViewCellSeparatorStyleNone; _productTableView.backgroundColor = [UIColor whiteColor]; _productTableView.dataSource = self; // _productTableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0); _productTableView.emptyDataSetSource = self; _productTableView.emptyDataSetDelegate = self; _headerView = [[ProductListHeaderView alloc] initWithFrame:CGRectMake(0, 0, ws.view.size.width, 44)]; _headerView.delegate = self; // [_productTableView setTableHeaderView:_headerView]; [self.view addSubview:_productTableView]; [_productTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(ws.view< 4000 /span>.size.width, SCREEN_HEIGHT-HEIGHT_NAV_STATUS_BAR)); make.top.mas_equalTo(ws.view.mas_top).with.offset(0); make.left.mas_equalTo(ws.view.mas_left).with.offset(0); }]; // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadMoreData方法) if (_search) { _productTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadDataSearch)]; } } #pragma mark - ProductTableHeaderDelegate -(void)didClickTab:(TableShowType)tabShowType asc:(BOOL)asc { if (TabPriceType == tabShowType) { _asc = asc; }else if(TableFilterType == tabShowType){ FilterView *filterView = [[FilterView alloc] initwithParentController:self]; [filterView setInfo]; return; } _tabShowType = tabShowType; [_proudctListArray removeAllObjects]; _pageIndex = 0; // [self loadDataSearch]; [self loadData]; } - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView { return [UIImage imageNamed:@"default_empty"]; } //加载数据的时候等待显示的动画 - (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"]; animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)]; animation.duration = 0.25; animation.cumulative = YES; animation.repeatCount = MAXFLOAT; return animation; } - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView { NSString *text = @"没有搜索结果"; NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:12.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]}; return [[NSAttributedString alloc] initWithString:text attributes:attributes]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (_search) { return 1; }else{ return 1; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (_search) { return _proudctListArray.count; }else{ // return _searchHistoryArray.count; return 10; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (_search) { static NSString *CellIdentifier = @"ProductListCell"; ProductListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[ProductListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleGray; } [cell setSeparator:indexPath.row ==0 middle:NO last:YES]; [cell setInfo:[_proudctListArray objectAtIndex:indexPath.row]]; return cell; }else{ static NSString * cellId = @"cellID"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; cell.selectionStyle = UITableViewCellSelectionStyleGray; tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; } cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row]; return cell; } } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (_search) { return 128.0f; }else return 44.0f; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (_search) { [tableView deselectRowAtIndexPath:indexPath animated:YES]; GoodsModel *goodsModel = _proudctListArray[indexPath.row]; ProductDetailViewController *detailController = [[ProductDetailViewController alloc] initWithGoodsId:goodsModel.goods_id]; [self bPushViewController:detailController animated:YES]; }else{ } } -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ [placeHolderLabel resignFirstResponder]; } #pragma mark - textFiled 搜索响应 -(void)searchButtonClick{ //取消textField第一响应者 [placeHolderLabel resignFirstResponder]; if ([placeHolderLabel.text isEqualToString:@""]) { [self showOnlyTextDialog:@"请输入关键字"]; }else{ _search = YES; // _headerView = [[ProductListHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.size.width, 44)]; // _headerView.delegate = self; [_productTableView setTableHeaderView:_headerView]; [self loadDataSearch]; [_productTableView reloadData]; } } //监听textFiled是否有内容 -(void)doaction{ if ([placeHolderLabel.text isEqualToString:@""]) { _search = NO; [_productTableView setTableHeaderView:nil]; [self loadData]; [_productTableView reloadData]; } }
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- winform异型不规则界面设计的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- WordPress中用于获取搜索表单的PHP函数使用解析
- JavaScript中数组的排序、乱序和搜索实现代码
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- Android中设置只有程序第一次运行才显示的界面实现思路
- PHP的历史和优缺点
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- hta 实现的五子棋界面