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::bringSubviewToFront didn't
work, which makes me believe that the label isn't a child of the searchResultsTableView at
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;
} // {…}
}
answered Aug 2 '12 at 8:39
relikd

7,48541956
   

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.

answered Jul 27 '12 at 19:53
MarkGranoff

10.2k2338
   

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

Regards, 

Amar

answered Jul 30 '12 at 3:44
Amar

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

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

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

How do I cover the “no results” text in UISearchDisplayController's searchResultTableView?的更多相关文章

  1. 自定义UISearchDisplayController的“No Results“标签和”Cancel“按钮

    本文转载至 http://www.cnblogs.com/pengyingh/articles/2350154.html - (void)searchDisplayControllerWillBegi ...

  2. Flutter中的Container和Text组件的常用属性

    效果图: import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends St ...

  3. UISearchDisplayController “No Results“ cancel修改

    Recently I needed to fully customize a UISearchBar, so here are some basic "recipes" on ho ...

  4. 斯坦福CS课程列表

    http://exploredegrees.stanford.edu/coursedescriptions/cs/ CS 101. Introduction to Computing Principl ...

  5. jQuery系列:Ajax

    1. load(url, [data], [callback]) 1.1 解析 载入远程 HTML 文件代码并插入至 DOM 中. 语法格式: load(url, [data], [callback] ...

  6. CSS & JS 制作滚动幻灯片

    ==================纯CSS方式==================== <!DOCTYPE html> <html> <head> <met ...

  7. 微信小程序--火车票查询

    微信小程序--火车票查询 写在最前面 微信小程序自九月份推出内测资格以来,经历了舆论热潮到现在看似冷清,但并不意味着大家不那么关注或者不关注了.我想不管是否有内测资格,只要是感兴趣的开发者已经进入潜心 ...

  8. JQuery常用方法一览

    $(”p”).addClass(css中定义的样式类型); 给某个元素添加样式 $(”img”).attr({src:”test.jpg”,alt:”test Image”}); 给某个元素添加属性/ ...

  9. HTTP POST 提交问题

    最近用http+post方式实现了系统间数据交互的需求. 常用的方式是 application/json方式直接post json对象 . 告诉服务器数据格式将会是 { Name : 'John Sm ...

随机推荐

  1. 详谈Windows消息循环机制

    一直对windows消息循环不太清楚,今天做个详细的总结,有说错的地方,请务必指出. 用VS2017新建一个win32 Application的默认代码如下: 程序入口                ...

  2. CentOS 7安装与配置JDK8

    1.检查是否安装过JDK 2.下载安装包并上传服务器 3.安装JDK rpm -ivh jdk-8u131-linux-x64.rpm 4.查看是否安装正常 java -version 5.配置环境变 ...

  3. python基础--迭代器、生成器、内置函数、面向对象编程

    迭代器:迭代器对象从集合的第一个元素开始访问,直到所有的元素都被访问完结束.迭代器只能往前不会后退 迭代:更新换代(重复)的过程,每次的迭代都必须基于上一次的结果 迭代器:迭代取值的工具 使用迭代器的 ...

  4. wordpress设置一个特定的页面作为首页

    首先在"页面"里新建一个页面,比如标题为"welcome"; 然后在设置里找到"阅读",首页显示调整为"一个静态页面", ...

  5. Git push 出错 [The remote end hung up unexpectedly] - 简书

    one day,my teamate using git push and occured this error. $ git push Counting objects: 2332669, done ...

  6. 一个基于swoole的作业调度组件,已经实现了redis和rabitmq队列消息存储。

    https://github.com/kcloze/swoole-jobs 一个基于swoole的作业调度组件,已经实现了redis和rabitmq队列消息存储.参考资料:swoole https:/ ...

  7. ThinkPHP实现定时执行任务的两种方法 - 博客频道 - CSDN.NET

    在平常的项目中我们总是会遇到需要将某个方法任务定时执行的问题,定时执行方法任务如果我们拥有服务器的权限,我们可以直接在服务器设置定时任务,例如在Windows的任务计划程序中进行设置,在Linux中编 ...

  8. js里面的this指向

    1.  this是动态绑定的,或者说是在代码运行期绑定而不是在书写期 function fire () { console.log(this.a) } var obj = { a: 1, fire: ...

  9. Codeforces 375A

    这是一道数学题,真是很考验数学思维,之前也遇到过相似的问题,但是依然是想不到点子上,就这提而言,最重要的就是 能否发现由 1, 6, 8,9这四个数字组成的排列对7取模是可以得到0, 1, 2, 3, ...

  10. linux安装软件报错: Can't locate ExtUtils/Embed.pm in @INC...

    安装snmp服务, 中间报错: Can't locate ExtUtils/Embed.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/l ...