ios怎样把uisearchbar添加uitabelview中

 我来答
杭天财料Dh
2016-09-02 · TA获得超过400个赞
知道小有建树答主
回答量:496
采纳率:0%
帮助的人:286万
展开全部
这里进行重新定制, 四个协议, 三个成员变量,第一步OK。
@interface ViewController ()<UISearchBarDelegate,UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>
{
UISearchBar *mySearchBar;
UISearchDisplayController *mySearchDisplayController;
NSMutableArray *suggestResults;
}
@end

//下面我们来初始化我们的三个成员
-(void)initMysearchBarAndMysearchDisPlay
{
mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, [UIApplicationsharedApplication].statusBarFrame.size.height, [UIScreen mainScreen].bounds.size.width, 44)];
mySearchBar.delegate = self;
//设置选项
mySearchBar.barTintColor = [UIColor redColor];
mySearchBar.searchBarStyle = UISearchBarStyleDefault;
mySearchBar.translucent = NO; //是否半透明
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mySearchBar sizeToFit];

mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:mySearchBar contentsController:self];
mySearchDisplayController.delegate = self;
mySearchDisplayController.searchResultsDataSource = self;
mySearchDisplayController.searchResultsDelegate = self;

suggestResults = [NSMutableArray arrayWithArray:@[@"此处为推荐词", @"也可以为历史记录"]];

}
//然后呢当然是自定义我们的navigation bar了
-(void)initNavigationBar
{
UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[moreButton setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
[moreButton addTarget:self action:@selector(handleMore:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *moreItem = [[UIBarButtonItem alloc] initWithCustomView:moreButton];

UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[searchButton setImage:[UIImage imageNamed:@"info_search"] forState:UIControlStateNormal];
[searchButton addTarget:self action:@selector(startSearch:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
self.navigationItem.rightBarButtonItems = @[moreItem, searchItem];
}
//好了, 开始加载界面了
- (void)viewDidLoad
{
[super viewDidLoad];
[self initNavigationBar];
[self initMysearchBarAndMysearchDisPlay];
}
//实现我们的点击事件
#pragma mark - handle button click
-(void) startSearch:(id)sender
{
[self.navigationController.view addSubview:mySearchBar];
[mySearchBar becomeFirstResponder];
}
-(void) handleMore:(id)sender
{}
//实现我们的table view 的协议,判断当我们开始搜索时,转到自己实现的一个searchDisplayController方法里,这样代码看起来是不是很简洁?
#pragma mark - UITableViewDataSource & UITableViewDelegate
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == mySearchDisplayController.searchResultsTableView)
{
return [self numberOfRowsWithSearchResultTableView];
}
return 0;
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == mySearchDisplayController.searchResultsTableView)
{
return [self searchTableView:mySearchDisplayController.searchResultsTableView cellForRowAtIndexPath:indexPath];
}
else
{
static NSString *cellID = @"search_cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
return cell;
}
}
//这里专门写UISearchDisplayController 中 searchResultsTableView 的 data source 和 delegate
#pragma mark - UISearchDisplayController <UITableViewDataSource> dataSource
-(NSInteger)numberOfRowsWithSearchResultTableView
{
return suggestResults.count + 1;
}
-(UITableViewCell*)searchTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *suggestId = @"suggestCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:suggestId];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:suggestId];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.row == suggestResults.count)
{
cell.textLabel.text = [NSLocalizedString(@"Search: ", @"查找: ") stringByAppendingString:mySearchBar.text];
}
else
{
cell.textLabel.text = [suggestResults objectAtIndex:indexPath.row];
}
return cell;
}
#pragma mark - UISearchDisplayController <UITableViewDelegate> delegate
-(void) searchTableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *keyword = nil;
if (indexPath.row == suggestResults.count)
{
keyword = mySearchBar.text;
}
else
{
keyword = [suggestResults objectAtIndex:indexPath.row];
}
[mySearchBar resignFirstResponder];
}
//开始我们的search bar delegate,输入搜索内容, 因为是Demo ,所以我并没有搜索结果,这个大家根据需求自己实现吧
#pragma mark - UISearchBarDelegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
return YES;
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[mySearchDisplayController.searchResultsTableView reloadData];

}

//最后的环节了, 这个地方的逻辑就会千变万化了
#pragma mark - UISearchDisplayDelegate
- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0)
{
//开始搜索事件,设置searchResultsTableView的contentInset,否则位置会错误
mySearchDisplayController.searchResultsTableView.contentInset = UIEdgeInsetsMake(mySearchBar.frame.size.height, 0, 0, 0);
}
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableViewNS_DEPRECATED_IOS(3_0,8_0)
{
//加载searchResultsTableView完毕
}
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0)
{
//结束搜索事件,移除mySearchBar
[mySearchBar removeFromSuperview];
}
// return YES to reload table. called when search string/option changes. convenience methods on top UISearchBar delegate methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString*)searchString NS_DEPRECATED_IOS(3_0,8_0)
{
//是否需要刷新searchResultsTableView
if(@"符合条件")
{
return YES;
}
return NO;

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式