IOS 自定义搜索框和结果列表

头文件:

#ifndef SearchWindowViewController_h
#define SearchWindowViewController_h


#import <UIKit/UIKit.h>

@interface SearchWindowViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, retain) UITableView *tv;
@property(nonatomic, retain) UITextField *tf;
@end

#endif /* SearchWindowViewController_h */

.m文件:

#import <Foundation/Foundation.h>
#import "SearchWindowViewController.h"


@interface SearchWindowViewController()<UITextFieldDelegate>
// 搜索前的原始数据数组
@property(nonatomic, retain) NSMutableArray *original;
// 搜索时输入的文字
@property(nonatomic, retain) NSString *search;
@end


@implementation SearchWindowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
}



-(void) initView{
//    隐藏默认左上角返回按钮
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.hidesBackButton = YES;


    _tf=[[UITextField alloc]init];
    //设置frmae
    _tf.width=370;
    _tf.height=35;
    _tf.backgroundColor=[UIColor whiteColor];
    _tf.placeholder = @"检索站内信";
    _tf.layer.cornerRadius = 9;
    _tf.layer.masksToBounds = YES;
    //设置文字内容垂直居中
    _tf.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    [_tf addTarget:self action:@selector(searchMethod) forControlEvents:UIControlEventEditingChanged];


    //设置左边的放大镜
    UIImageView *leftView=[[UIImageView alloc]init];
    //设置leftView的frame
    leftView.width=30;
    leftView.height=30;
    leftView.image=[UIImage imageNamed:@"searchbar_icon_search"];
    leftView.backgroundColor = [UIColor clearColor];
    _tf.leftView=leftView;
    _tf.delegate = self;

    //设置leftViewMode
    _tf.leftViewMode=UITextFieldViewModeAlways;
    //设置放大镜距离左边的间距,设置leftView的内容居中
    leftView.contentMode=UIViewContentModeCenter;

    //添加到导航栏中
    self.navigationItem.titleView=_tf;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setTitle:@"取消" forState:UIControlStateNormal];

    // 设置rightBarButtonItem
    UIBarButtonItem *rightItem =[[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    _original = [[NSMutableArray alloc] initWithObjects:@"叶小丽", @"王小建",
                      @"王大才", @"高老师", @"江老师", @"刘博士", @"AI-Ghanim,MJ", @"zhao yuhui", nil];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    if (!cell)
    {
        //如果不存在,创建一个可重用cell
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:nil];
    }
    
    for(int i=0; i<_original.count; ++i){
        //寻找关键词
        if ([_original[i] rangeOfString:_search].location != NSNotFound) {
            cell.textLabel.text = _original[i];
        }
    }
    
    return cell;
}


-(void) cancelBtnClick:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:NO];
}

-(void) searchMethod{
    NSLog(@"search!!");
    
    _tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    _tv.delegate = self;
    _tv.dataSource = self;
    [self.view addSubview:_tv];
    
    
    if (_tf.text.length > 0)
    {
        // UITextField有输入
        _search = _tf.text;
    }
    else{
        // UITextField没有输入
        _search = @"";
    }
    [self.tv reloadData];
}



@end

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的搜索框示例,包含自定义搜索引擎功能: HTML代码: ``` <div class="search-container"> <input type="text" id="search-input" placeholder="Search..."> <button class="search-button" onclick="search()">Search</button> <div class="search-engines"> <button class="engine-button" onclick="selectEngine()">Google</button> <button class="engine-button" onclick="selectEngine()">Bing</button> <button class="engine-button" onclick="selectEngine()">Yahoo</button> <button class="engine-button" onclick="selectCustomEngine()">Add</button> <div id="custom-engine" class="hidden"> <input type="text" id="engine-name" placeholder="Enter engine name"> <input type="text" id="engine-url" placeholder="Enter engine search url"> <button class="add-button" onclick="addEngine()">Add Engine</button> </div> </div> </div> ``` CSS代码: ``` .search-container { display: flex; align-items: center; } #search-input { flex: 1; padding: 6px; font-size: 16px; border: none; border-radius: 4px; background-color: #f1f1f1; } .search-button { margin-left: 6px; padding: 6px 12px; font-size: 16px; border: none; border-radius: 4px; background-color: #4CAF50; color: #fff; cursor: pointer; } .search-engines { position: relative; } .engine-button { margin-left: 6px; padding: 6px 12px; font-size: 16px; border: none; border-radius: 4px; background-color: #f1f1f1; cursor: pointer; } #custom-engine { position: absolute; top: 100%; left: 0; margin-top: 6px; padding: 6px; border-radius: 4px; background-color: #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.2); } .hidden { display: none; } ``` JS代码: ``` var searchUrl = 'https://www.google.com/search?q='; function search() { var searchInput = document.getElementById('search-input'); var query = searchInput.value; window.location.href = searchUrl + encodeURIComponent(query); } function selectEngine() { // TODO: set searchUrl based on selected engine // example: searchUrl = 'https://www.bing.com/search?q='; } function selectCustomEngine() { var customEngine = document.getElementById('custom-engine'); customEngine.classList.toggle('hidden'); } function addEngine() { var engineName = document.getElementById('engine-name').value; var engineUrl = document.getElementById('engine-url').value; if (engineName && engineUrl) { var engines = document.getElementsByClassName('search-engines')[0]; var button = document.createElement('button'); button.className = 'engine-button'; button.textContent = engineName; button.onclick = function() { searchUrl = engineUrl; selectEngine(); } engines.insertBefore(button, engines.lastChild); var customEngine = document.getElementById('custom-engine'); customEngine.classList.add('hidden'); } } ``` 上面的代码展示了一个简单的搜索框,包含一个文本输入框、搜索按钮和选择搜索引擎的按钮。点击搜索按钮会将查询字符串添加到搜索引擎的URL中,并打开新页面进行搜索。选择搜索引擎时,可以通过设置全局变量searchUrl来确定所选引擎的URL。添加自定义搜索引擎时,用户可以在弹出的表单中输入引擎名称和搜索URL,并将其添加为选项之一。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值