Zar*_*ony 42 iphone objective-c uisearchbar searchdisplaycontroller
通常,UISearchDisplayController在激活时会使tableView变暗并聚焦searchBar.只要在searchBar中输入文本,它就会创建一个searchResultsTableView,它在searchBar和键盘之间显示.当第二个UITableView被加载/显示/隐藏/卸载时,将调用searchDisplayController的委托.通常它会在输入时显示实时搜索结果或自动完成条目.
在我的应用程序中,我想搜索Web服务,我不想为用户输入的每个字母调用webservice.因此,我想完全禁用searchResultsTableView并在输入文本时保持暗灰色覆盖.然后,一旦他点击搜索按钮,我就会触发搜索(带有加载屏幕).
只返回searchResultsTableView的零行看起来不太好,因为它显示一个带有"无结果"消息的空searchResultsTableView.我试图在它出现时隐藏表(searchDisplayController:didLoadSearchResultsTableView:
),但是黑色的灰色叠加层也是隐藏的,这样底层的tableView再次完全可见.
除了从头开始重新创建UISearchDisplayController功能之外的任何想法?
小智 38
这是一个我想通的小技巧,你还需要在编辑searchstring时返回0结果
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
savedSearchTerm = searchString;
[controller.searchResultsTableView setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.8]];
[controller.searchResultsTableView setRowHeight:800];
[controller.searchResultsTableView setScrollEnabled:NO];
return NO;
}
- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView
{
// undo the changes above to prevent artefacts reported below by mclin
}
Run Code Online (Sandbox Code Playgroud)
我想你会明白下一步该做什么
小智 19
你试过这个:
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_lookup:) object:nil];
[self performSelector:@selector(_lookup:) withObject:txt afterDelay:0.20];
Run Code Online (Sandbox Code Playgroud)
这样,如果用户在1/5秒内键入另一个char,则只进行一次Web调用.
上面的任何内容似乎都不能正常工作,所以我想出了以下内容(当你准备好显示你的结果时,你必须调用removeTableHeader):
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
[self setTableHeader];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
[self setTableHeader];
}
- (void)setTableHeader {
UIView *headerView = [[UIView alloc] initWithFrame:self.searchDisplayController.searchResultsTableView.frame];
headerView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8];
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor clearColor]];
[self.searchDisplayController.searchResultsTableView setScrollEnabled:NO];
[self.searchDisplayController.searchResultsTableView setTableHeaderView:headerView];
[headerView release];
}
- (void)removeTableHeader {
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor whiteColor]];
[self.searchDisplayController.searchResultsTableView setScrollEnabled:YES];
[self.searchDisplayController.searchResultsTableView setTableHeaderView:nil];
}
Run Code Online (Sandbox Code Playgroud)
显然,它使表格透明,添加一个与表格大小相同的黑色/半透明表格标题,并禁用表格上的滚动,这样就不会超出或超过标题.作为奖励,您可以在标题视图中添加一些内容('请稍候......'或活动指示符).
遇到和你一样的问题,我通过a)在开始搜索时将searchResultsTableView的alpha设置为0,然后通过b)添加/删除overlayView到viewController的视图来处理它.对我来说就像一个魅力.
@interface MyViewController()
//...
@property(nonatomic, retain) UIView *overlayView;
//...
@end
@implementation MyViewController
@synthesize overlayView = _overlayView;
//...
- (void)viewDidLoad
{
//...
//define your overlayView
_overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 480)];
_overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
}
//hide the searchResultsTableView
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
self.searchDisplayController.searchResultsTableView.alpha = 0.0f;
}
//when ending the search, hide the overlayView
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
[_overlayView removeFromSuperview];
}
//depending on what the user has inputed, add or remove the overlayView to the view of the current viewController
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
if ([searchString length]>0)
{
[self.view addSubview:_overlayView];
}
else
{
[_overlayView removeFromSuperview];
}
return NO;
}
@end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
25743 次 |
最近记录: |