您的位置:首页 > 产品设计 > UI/UE

搜索栏有内容搜索商品,清空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];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  界面 搜索 历史