iOS 两种搜索框UISeacherBar
2015-06-27 本文已影响5783人
Figo_OU
两种UISeacherBar,一种是系统自带的,另一种是利用UITextField做的。这个搜索功能可以用拼音来搜索(自己看demo吧)
- 先上图
- 利用系统的UISeacherBar的话,需要设置代理,然后还需要有个
UISearchDisplayController来装搜索到的结果。并设置代理和数据源。
- (void)initSearchTextField
{
SearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 44)];
SearchBar.delegate = self;
SearchBar.exclusiveTouch = YES;
[SearchBar setPlaceholder:@"输入城市名"];
resultController = [[UISearchDisplayController alloc] initWithSearchBar:SearchBar contentsController:self];
resultController.searchResultsDataSource = self;
resultController.searchResultsDelegate = self;
}
- 而在自定义的搜索框,则需要设置
UITextField
的UIControlEventEditingChanged
事件。
如:
[searchTextFiled addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
然后在textFieldChanged
函数中作想要做的事情。