1. 程式人生 > >使用UITextField實現搜尋功能

使用UITextField實現搜尋功能

    使用UISearchBar實現搜尋功能時,搜尋欄高度不易調整,外觀也不易做成自己想要的樣式,又不想使用太複雜的方法,而使用UITextField可以實現這些功能又非常簡便,所以使用UITextField是個不錯的選擇。

     下面實現0-10000的數字搜尋

1,在.h檔案中實現UITableView的協議,用UITableView和UITextField共同實現搜尋功能。

@interface ViewController : UIViewController < UITableViewDelegate, UITableViewDataSource>

2,定義全域性變數,

allNumberArr儲存0-10000,filterNumberArr儲存搜尋過濾後的陣列

@interface ViewController () {
    UITextField *searchTextField;
    
    NSMutableArray *allNumberArr;
    NSMutableArray *filterNumberArr;
    
    UITableView *searchTableView;
}

3,初始化allNumberArr和filterNumberArr中的資料

- (void)initialAllNumber {
    allNumberArr = [[NSMutableArray alloc] init];
    filterNumberArr = [[NSMutableArray alloc] init];
    
    for (int i = 0; i <= 10000; i++) {
        NSString *numberStr = [[NSString alloc] initWithFormat:@"%d", i];
        [allNumberArr addObject:numberStr];
        [filterNumberArr addObject:numberStr];
    }
}

4,初始化UITalbeView,設定委託

//在willDidLoad中呼叫
- (UITableView *)createTableViewWithFrame:(CGRect)frame {
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame];
    tableView.delegate = self;
    tableView.dataSource = self;
    return tableView;
}

5,建立UITextField搜尋欄和搜尋欄所在的View,調整View的外觀,作為搜尋欄的外觀。調整起來比較容易。在View中加入UITextField,加入文字修改事件。search圖示自己找的

- (UIView *)createSearchViewWithFrame:(CGRect)frame {
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.layer.borderWidth = 1;
    view.layer.borderColor = [UIColor blueColor].CGColor;
    view.layer.cornerRadius = 20;
    
    return view;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    [self initialAllNumber];
    
    UIView *searchView = [self createSearchViewWithFrame:CGRectMake(50, 40, self.view.frame.size.width - 100, 40)];
    [self.view addSubview:searchView];
    
    searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 3, searchView.frame.size.width - 50, searchView.frame.size.height - 6)];
    [searchTextField addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
    [searchView addSubview:searchTextField];
    
    UIImageView *searchImgView = [[UIImageView alloc] initWithFrame:CGRectMake(searchView.frame.size.width - 40, 10, 20, 20)];
    searchImgView.image = [UIImage imageNamed:@"search"];
    [searchView addSubview:searchImgView];
    
    
}

6,編輯UITextField修改文字時的事件,當文字修改時,當allNumberArr中的資料包含searchTextField.text,將allNumberArr中的資料加入到filterNumberArr中,並重新整理TableView.

- (void)textFieldDidChange {
    [filterNumberArr removeAllObjects];
    
    for (int i = 0; i < allNumberArr.count; i++) {
        if ([allNumberArr[i] containsString:searchTextField.text]) {
            [filterNumberArr addObject:allNumberArr[i]];
        }
    }
    
    [searchTableView reloadData];
}

 7,實現UITalbeViewDelegate和UITalbeViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return filterNumberArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    
    cell.textLabel.text = filterNumberArr[indexPath.row];
    
    return cell;
}

 實現結果為