博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How do I cover the “no results” text in UISearchDisplayController's searchResultTableView?
阅读量:4337 次
发布时间:2019-06-07

本文共 4571 字,大约阅读时间需要 15 分钟。

I don't want to show the "no results" text while my server is processing a search query.

enter image description here

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::bringSubviewToFront didn't work, which makes me believe that the label isn't a child of the searchResultsTableView at all.

BTW, this  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; } // {…}}
|
   
0

You need to realize that when you have a UISearchDisplayController, and the search bar is active, the UITableView argument passed into your UITableView data source and delegate methods is in fact NOT your tableView object, but a tableView managed by theUISearchDisplayController, 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 theUISearchDisplayController displays 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.

|
   
0

I haven't tried it myself, you can give it a try-- 

Regards, 

Amar

|
   
因为产生的UILabel@“No Resulte”有延迟,如果不延迟检测是检测不出来的
-2

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;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/zsw-1993/p/4879967.html

你可能感兴趣的文章
spring mybatis解决application.properties注入变量问题
查看>>
signal(SIGPIPE, SIG_IGN)
查看>>
思软OA办公系统合同管理解决方案之合同管理系统产品理念
查看>>
Java常用的运算符
查看>>
Android学习笔记之控件GridView
查看>>
oracle sql语句运行效率
查看>>
RunRunLoop初识
查看>>
.NET正则基础之——平衡组
查看>>
mybatis 详解(三)------入门实例(基于注解)
查看>>
51Nod1336 RMQ逆问题 其他
查看>>
zoj1709 poj1562
查看>>
Android 实现 HttpClient 请求Https
查看>>
JSTL标签概述
查看>>
AngularJS中的DOM与事件
查看>>
Gitlab的使用
查看>>
es6-promise源代码重点难点分析
查看>>
滚动元素 <marquee></marquee>
查看>>
如何正确的判断Session变量是否存在
查看>>
JWT
查看>>
Python之路-python(堡垒机)
查看>>