美文网首页
iOS_搜索记录,历史搜索排版、记录

iOS_搜索记录,历史搜索排版、记录

作者: Qy_iOS | 来源:发表于2020-08-17 11:44 被阅读0次

    textfiled的创建搜索框,在左侧添加图片

        CGFloatviewWith =self.bounds.size.width;

        _searchTf= [[UITextFieldalloc]initWithFrame:CGRectMake(20*unitPX,STATUS_BAR_HEIGHT+4, viewWith -80*unitPX,35)];

        _searchTf.borderStyle = UITextBorderStyleNone;

        _searchTf.backgroundColor=PHDJColor(243,243,243);

        _searchTf.layer.masksToBounds = YES;

        _searchTf.layer.cornerRadius = 17;

        _searchTf.placeholder = @"搜索商品";

        [self addSubview:_searchTf];

        UIImage*im = [UIImageimageNamed:@"ic_search"];

        _searchIconImg = [[UIImageView alloc] initWithFrame:CGRectMake(10*unitPX, 0, 20, 20)];

        _searchIconImg.image = im;

        UIView*leftView = [[UIViewalloc]initWithFrame:CGRectMake(15*unitPX,0,40*unitPX,20)];

        [leftViewaddSubview:_searchIconImg];

        _searchTf.leftViewMode = UITextFieldViewModeAlways;

        _searchTf.leftView= leftView;

    代码:

    // 搜索历史,布局

    -(void)creatHistoryView

    {

        CGFloatnavigationHight =STATUS_BAR_HEIGHT+50;

        // 显示搜索记录的底部view

        _historyView= [[UIViewalloc]init];

        _historyView.frame=CGRectMake(0, navigationHight,SCREEN_WIDTH,200*unitPX);

        _historyView.backgroundColor = [UIColor whiteColor];

        [self.view addSubview:_historyView];

        // title

        UILabel*historyLabel = [[UILabelalloc]initWithFrame:CGRectMake(20*unitPX,10*unitPX,200,30*unitPX)];

        historyLabel.text=@"历史搜索";

        historyLabel.font= [UIFontfontWithName:@"Helvetica-Bold"size:15];

        [_historyViewaddSubview:historyLabel];

        // 清除搜索记录

        UIButton *cleanBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        cleanBtn.frame = CGRectMake(SCREEN_WIDTH - 45*unitPX, 12*unitPX, 25*unitPX, 25*unitPX);

        cleanBtn.backgroundColor = [UIColor cyanColor];

        [cleanBtnaddTarget:self action:@selector(cleanHistoryKeyWord) forControlEvents:UIControlEventTouchUpInside];

        [_historyViewaddSubview:cleanBtn];

        // 取搜索历史记录

        _historyArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"keywordSearchHistory"];

        _historyArr = [[_historyArr reverseObjectEnumerator] allObjects];

        _btnLeftW = 15*unitPX;  // 保存前一个button的宽以及前一个button距离屏幕边缘的距离

        _btnTopH = 45*unitPX;  // 用来控制button距离父视图的高

        // 如果没有搜索记录,显示提示label

        if(_historyArr.count==0) {

            _noHistoryLabel= [[UILabelalloc]initWithFrame:CGRectMake(30*unitPX,_btnTopH,200,30*unitPX)];

            _noHistoryLabel.text = @"无搜索记录";

            _noHistoryLabel.textColor = [UIColor grayColor];

            _noHistoryLabel.font = [UIFont systemFontOfSize:13];

            [_historyView addSubview:_noHistoryLabel];

        }else{

            for(inti =0; i <_historyArr.count; i++) {

                _historyBtn = [UIButton buttonWithType:UIButtonTypeCustom];

                _historyBtn.backgroundColor=PHDJColor(245,245,245);

                [_historyBtnsetTitleColor:PHDJColor(32,32,32)forState:0];

                _historyBtn.titleLabel.font = [UIFont systemFontOfSize:13];

                _historyBtn.tag=100+i;

                _historyBtn.layer.cornerRadius=15*unitPX;

                _historyBtn.layer.masksToBounds=YES;

                [_historyBtn addTarget:self action:@selector(historyBtnClick:) forControlEvents:UIControlEventTouchUpInside];

                //根据计算文字的大小

               NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};

               CGFloat length = [_historyArr[i] boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;

                [_historyBtnsetTitle:_historyArr[i]forState:0];

                _historyBtn.frame=CGRectMake(10*unitPX+_btnLeftW,_btnTopH, length +20*unitPX,30*unitPX);

                //当button的位置超出屏幕边缘时换行

                if(10*unitPX+_btnLeftW+ length +20*unitPX>SCREEN_WIDTH-30*unitPX){

                    _btnLeftW=15*unitPX;//换行时将w置为15

                    _btnTopH=_btnTopH+_historyBtn.frame.size.height+10*unitPX;//距离父视图也变化

                    _historyBtn.frame=CGRectMake(10*unitPX+_btnLeftW,_btnTopH, length +20*unitPX,30*unitPX);//重设button的frame

                }

                _btnLeftW = _historyBtn.frame.size.width + _historyBtn.frame.origin.x;

                [_historyView addSubview:_historyBtn];

            }

        }

        // 通过搜索历史记录的行数,最终重新计算搜索历史底部view的高度

        _historyView.frame=CGRectMake(0, navigationHight,SCREEN_WIDTH,_btnTopH+45*unitPX);

    //    [self hotSearchView];

    }

    数据记录,逻辑处理

    // 点击搜索

    - (BOOL)textFieldShouldReturn:(UITextField *)textField{

        // 空格处理

        NSString *textLength = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];

        if(textLength.length==0) {

            returnNO;

        }

        // 存

        NSMutableArray * searhArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"keywordSearchHistory"];

        if(!searhArray) {

            searhArray = [NSMutableArrayarray];

        }

        searhArray = [searhArraymutableCopy];

        if([searhArraycontainsObject: textLength]) {

            [searhArrayremoveObject:textLength];

        }

        [searhArrayaddObject:textLength];

        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"keywordSearchHistory"];

        [[NSUserDefaults standardUserDefaults] setObject:searhArray forKey:@"keywordSearchHistory"];

        PHGoodsListViewController *goodsListVC = [[PHGoodsListViewController alloc] init];

        [self.navigationController pushViewController:goodsListVC animated:YES];

        return YES;

    }

    // 清空历史记录

    -(void)cleanHistoryKeyWord{

        // 删除

        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"keywordSearchHistory"];

        if(_historyArr.count>0) {

            [_historyView removeFromSuperview];

            [self creatHistoryView];

        }

        NSLog(@"clean_cleanHistoryKeyWord");

    }

    // 搜索历史记录点击

    -(void)historyBtnClick:(UIButton*)sender{

        NSIntegeri = sender.tag-100;

        NSLog(@"%ld",i);

        // 存

        NSMutableArray * searhArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"keywordSearchHistory"];

        if(!searhArray) {

            searhArray = [NSMutableArrayarray];

        }

        searhArray = [searhArraymutableCopy];

        if([searhArraycontainsObject:_historyArr[i]]) {

            [searhArrayremoveObject:_historyArr[i]];

        }

        [searhArrayaddObject:_historyArr[i]];

        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"keywordSearchHistory"];

        [[NSUserDefaults standardUserDefaults] setObject:searhArray forKey:@"keywordSearchHistory"];

        PHGoodsListViewController *goodsListVC = [[PHGoodsListViewController alloc] init];

        [self.navigationController pushViewController:goodsListVC animated:YES];

    }

    相关文章

      网友评论

          本文标题:iOS_搜索记录,历史搜索排版、记录

          本文链接:https://www.haomeiwen.com/subject/raafjktx.html