How do I cover the “no results” text in UISearchDisplayController's searchResultTableView?
2013-07-11 14:22
686 查看
How
do I cover the “no results” text in UISearchDisplayController's searchResultTableView?
I don't want to show the "no results" text while my server is processing a search query.I figured out the exact coordinates of the table cell that contains the label and attempted to cover it.
self.noResultsCoverView = [[[UIView alloc] initWithFrame:CGRectMake( 0.0, 44.0, 320.0, 43.0 )] autorelease]; self.noResultsCoverView.backgroundColor = [UIColor whiteColor]; [self.searchDisplayController.searchResultsTableView addSubview:self.noResultsCoverView];
To my chagrin, my cover was above the table view, but below the label. I need the cover to be above the label.
searchResultsTableView::bringSubviewToFrontdidn't
work, which makes me believe that the label isn't a child of the
searchResultsTableViewat
all.
BTW, this Stack Overflow answer doesn't quite work for me.
It works on the very first search, but flashes a weird black cover on subsequent searches.
this should do the work properly. The code to return at least one cell:BOOL ivarNoResults; // put this somewhere in @interface or at top of @implementation - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.searchDisplayController.searchResultsTableView) { if (filteredList.count == 0) { ivarNoResults = YES; return 1; } else { ivarNoResults = NO; return [filteredList count]; } } // {…} // return the unfiltered array count } and for "showing" the clean cell: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.searchDisplayController.searchResultsTableView && ivarNoResults) { static NSString *cleanCellIdent = @"cleanCell"; UITableViewCell *ccell = [tableView dequeueReusableCellWithIdentifier:cleanCellIdent]; if (ccell == nil) { ccell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cleanCellIdent] autorelease]; ccell.userInteractionEnabled = NO; } return ccell; } // {…} }
| ||
up vote0down vote | You need to realize that when you have a UISearchDisplayController, and the search bar is active, the UITableViewargument passed into your UITableViewdata source and delegate methods is in fact NOT your tableView object, but a tableView managed by the UISearchDisplayController, intended to display "live" search results (perhaps results filtered from your main data source, for example). You can easily detect this in code, and then return the appropriate result from the delegate/data source method, depending on which tableView object is asking. For example: - (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section { if (tv == self.searchDisplayController.searchResultsTableView) { // return the number of rows in section for the visible search results. // return a non-zero value to suppress "No results" } else { // return the number of rows in section for your main data source } } The point is that your data source and delegate methods are serving two tables, and you can (and should) check for which table is asking for data or delegation. By the way, the "No results" is (I believe) provided by a background image which the UISearchDisplayControllerdisplays when the delegate says there are no rows... You are not seeing a 2-row table, the first blank and the second with text "No results". At least, that's what I think is happening there.
| ||
up vote0down vote | I haven't tried it myself, you can give it a try-- Link Regards, Amar
| ||
up vote-2down vote | Try this it worked for me In the UISearchDisplayController delegate do this:= - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.001); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ for (UIView* v in self.searchDisplayController.searchResultsTableView.subviews) { if ([v isKindOfClass: [UILabel class]] && [[(UILabel*)v text] isEqualToString:@"No Results"]) { [(UILabel*)v setText:@""]; break; } } }); return YES; } |
相关文章推荐
- UISearchDisplayController进行搜索时,searchResultsTableView的contentSize不断加长的问题
- UISearchDisplayController的searchResultsTableView的Contentsize不正确的问题
- UISearchBar 和 UISearchDisplayController 没有在UITableView 的headerView 导致searchResultsTableView的frame出问题
- UISearchDisplayContoller – can't prevent table reload on typing in search bar
- 修复UISearchDisplayControllerd的searchResultsTableView的contentSize不正确
- uisearchDisplayController放在tableview里下拉刷新状态出现遮挡
- In an iOS 5 Storyboard, how do you push a new scene to the original view controller from a Popover?
- UISearchDisplayController “No Results“ cancel修改
- IOS:自定义UISearchDisplayController的cancel和no results
- UISearchDisplayController “No Results“ cancel修改
- ios UISearchDisplayController 实现 UITableView 搜索功能
- iOS--UISearchBar和UISearchDisplayController
- How To Add Search Bar in Table View
- UISearchDisplayController “No Results“ cancel修改
- How do I add placeholder text from the model into a MVC view?
- 设置searchDisplayController的searchResultsTableView为grouped
- UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
- UISearchDisplayController “No Results“ cancel修改
- UISearchDisplayController 去除“No Results”,“无结果” 的显示方法
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)