【发布时间】:2014-05-21 08:13:55
【问题描述】:
我在UIViewController 中嵌入了一个UICollectionview。我还向 UIViewController 添加了一个搜索栏,就像在 UICollectionview 中一样,我不知道通过 Storyboard 将它放在哪里。
我期待在UICollectionview 中搜索会像在Tableview 中一样简单。但它似乎有自己的规则。有没有什么好的和简单的例子来展示如何在UICollectionview 中实现简单的搜索,就像在UITableview 中一样?
我想实现用户可以在搜索栏中输入,UICollectionview 显示结果。
我找到了实现searchbar的解决方案:
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.collectionView.frame), 44)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.delegate = self;
[self.collectionView addSubview:self.searchBar];
[self.collectionView setContentOffset:CGPointMake(0, 44)];
}
- (void) viewWillAppear:(BOOL)animated{
// to show search bar
[self.collectionView setContentOffset:CGPointMake(0, 0)];
// to hide search bar
[self.collectionView setContentOffset:CGPointMake(0, 44)];
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[searchBar setText:@""];
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
}
这很好用,它会显示一个searchbar,这是一个好的开始,但现在我再也看不到我的标题了,因为searchbar 位于标题上方。知道如何把它放在标题上方吗?绿色的是标题。
【问题讨论】:
标签: ios search uiviewcontroller uicollectionview