iOS自定義搜索框

這是一個仿原生的搜索框,由于時間緊迫就簡單的實現了一下功能。廢話不多說,先上效果圖。


SearchDemo.gif

下面上代碼:
首先是搜索界面的.m

@interface ViewController ()
//搜索框/
@property (nonatomic, strong) UITextField *textField;
//展示視圖的數組/
@property (nonatomic, strong) NSMutableArray     *dataArray;
@end

@implementation ViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.textField.text = @"";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor brownColor];
    self.navigationItem.titleView = self.textField;
}

#pragma mark -- UITtableViewdelegate
-(NSInteger)tableView:(UITableView *)tableView  numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView     cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.textLabel.text = self.dataArray[indexPath.row];

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.textField resignFirstResponder];
}

#pragma mark --
- (void)textFieldTextChange:(UITextField *)textField{
    //  通過謂詞匹配要查找的內容
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",self.textField.text];
    //  查詢結果數組
    NSArray *array = [NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:predicate]];

    JYTableViewController *resultTableViewController = [[JYTableViewController alloc]initWithStyle:UITableViewStylePlain];
     //  將本界面數據傳入到結果控制器
        //  輸入框內容
    resultTableViewController.textFieldContent = textField.text;
        //  結果數組
    resultTableViewController.resultArray = array;
        //  本界面展示數據的數組,傳遞此數據時為了在為了用來匹配結果控制器輸入框的內容
    resultTableViewController.dataArray = self.dataArray;
    //  跳轉
    [self.navigationController pushViewController:resultTableViewController animated:NO];
}

#pragma mark -- lozy lode
-(UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc]init];
        _textField.placeholder = @" 搜索                                                         ??";
        _textField.tintColor = [UIColor purpleColor];
        _textField.textAlignment = NSTextAlignmentLeft;
        [_textField addTarget:self action:@selector(textFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
      _textField.frame = CGRectMake(0, 0, screenWidth - 80, 44);
    }
    return _textField;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithCapacity:3];
        for (NSInteger i = 0; i < 20; i++) {
            [_dataArray addObject:[NSString stringWithFormat:@"BLACKSKY __ %ld",i]];
        }
    }
    return _dataArray;
}

然后是結果控制器的.h文件

@interface JYTableViewController : UITableViewController
//  輸入框的內容(將上界面輸入框的內容傳過來)
@property (nonatomic, copy) NSString *textFieldContent;
//*查詢結果數據*/
@property (nonatomic, strong) NSArray *resultArray;
//*查詢數據要從這里匹配*/
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

下面是結果控制的.m文件

@interface JYTableViewController ()
//搜索框/
@property (nonatomic, strong) UITextField *textField;
@end

@implementation JYTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //  添加基本界面
    self.navigationItem.titleView = self.textField;
    UIBarButtonItem *cancleButton = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancleButton)];
    self.navigationItem.rightBarButtonItem = cancleButton;
    self.navigationItem.hidesBackButton = YES;
    [self.textField becomeFirstResponder];
    self.textField.text = self.textFieldContent;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.resultArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *reuseIdentifier = @"reuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    cell.textLabel.text = self.resultArray[indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.textField resignFirstResponder];
}

#pragma mark --
- (void)cancleButton{
    [self.textField resignFirstResponder];
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)textFieldTextChange:(UITextField *)textField{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",self.textField.text];
    NSArray *array = [NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:predicate]];
    self.resultArray = array;
    //  不要忘記刷新界面
    [self.tableView reloadData];
    //  當本輸入框中的內容為空的時候返回
    if (self.textField.text.length == 0) {
        [self.textField resignFirstResponder];
        [self.navigationController popViewControllerAnimated:NO];
    }
}

#pragma mark -- lozy lode
-(UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc]init];
        //  這里懶省事就用設置一下占位,實際中可以設置  _textField.leftView和_textField.rightView來展現界面
        _textField.placeholder = @" 搜索                                                         ??";
        _textField.tintColor = [UIColor purpleColor];
        _textField.textAlignment = NSTextAlignmentLeft;
        [_textField addTarget:self action:@selector(textFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
        _textField.frame = CGRectMake(0, 0, screenWidth - 80, 44);
    }
    return _textField;
}

@end

說句真心話,這些都是一點基礎的東西。我明白它并不會能給被人帶來什么幫助,但一定會給自己很多好處,無論是對iOS的喜好態度還是能力,thanks for。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容