使用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;
}
實現結果爲