【发布时间】:2016-01-07 06:30:23
【问题描述】:
您好,我在tableview 中使用UISerchbar,但有什么简单的方法可以在tableview headerview 中创建自定义searchbar?
我已经检查了下面的链接,但我不明白。
Custom UISearchBar with UISearchController
How do I use the UISearchBar and UISearchDisplayController
我添加了 searchDisplayController : :
这是我的 searchController 代码,
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
[self.searchController.searchBar setBarTintColor:[UIColor darkGrayColor]];
self.tableView.tableHeaderView = self.searchController.searchBar;
我也为UISearchBarDelegate,UISearchResultsUpdating 设置了委托,
并实现了他们的方法,
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
if (_searchController.isActive && _searchController.searchBar.text.length >0){
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF.name contains[cd] %@",
searchText];
abcd = [sortedEventArray filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%@",searchResults);
}
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController1
{
NSString *searchString = searchController1.searchBar.text;
[self filterContentForSearchText:searchString scope:@"abc"];
//[self filterContentForSearchText:searchString :@"abc"];
[self.tableView reloadData];
}
取消按钮事件
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
abcd=[sortedEventArray mutableCopy];
[_tableView reloadData];
}
-(void)delete{
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest * allMovies = [[NSFetchRequest alloc] init];
[allMovies setEntity:[NSEntityDescription entityForName:@"Data" inManagedObjectContext:context]];
[allMovies setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSError * error = nil;
NSArray * movies = [context executeFetchRequest:allMovies error:&error];
//error handling goes here
for (NSManagedObject * movie in movies) {
[context deleteObject:movie];
}
NSError *saveError = nil;
[context save:&saveError];
}
还是不行
提前致谢:)
【问题讨论】:
-
我为此工作了很长时间,最后决定将搜索栏放在桌子正上方的视图中会更容易。使用自动布局,很容易控制定位并将标题始终定位在同一位置,IIRC 是问题所在。我放弃了。祝你好运找到解决方法(我想我当时在 ios7 上)。
标签: ios objective-c uitableview uisearchbar