在storyboard中的静态UITableView中拖入 UISearchBar and Search Display Controller出现的奇怪问题
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGVib2xlZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
"WBDiscoverTableViewController.h"
@interface
WBDiscoverTableViewController ()
@property (weak,
nonatomic)
IBOutlet
UISearchBar *mySearchbar;
@property (nonatomic,
strong)
NSArray *results;
@property (weak,
nonatomic)
IBOutlet
UITableViewCell *hotTopicsCell1;
@property (weak,
nonatomic)
IBOutlet
UITableViewCell *hotTopicsCell2;
@property (weak,
nonatomic)
IBOutlet
UITableViewCell *nearbyPeopleCell;
@property (weak,
nonatomic)
IBOutlet
UITableViewCell *nearbyWeiboCell;
@end
@implementation WBDiscoverTableViewController
- (void)viewDidLoad {
viewDidLoad];
static
NSString *cellID =
@"resultCell";
[self.searchDisplayController.searchResultsTableView
registerClass:[UITableViewCell
class]
forCellReuseIdentifier:cellID];
}
- (void)viewWillAppear:(BOOL)animated
{
}
{
switch (self.mySearchbar.selectedScopeButtonIndex)
{
case
0:
//搜用户
if ([[NSUserDefaults
standardUserDefaults]
objectForKey:@"accessToken"])
{
[[WBWeiboAPI
shareWeiboApi]
searchSuggestionsUsersWithString:self.mySearchbar.text
AndCount:20
CompletionCallBack:^(id
obj) {
self.results
= obj;
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"self.results.count
:%ld", self.results.count);
[self.searchDisplayController.searchResultsTableView
reloadData];
});
}];
}
break;
case
1:
//搜学校
if ([[NSUserDefaults
standardUserDefaults]
objectForKey:@"accessToken"])
{
[[WBWeiboAPI
shareWeiboApi]
searchSuggestionsSchoolsWithString:self.mySearchbar.text
AndCount:20
AndType:0
CompletionCallBack:^(id
obj) {
self.results
= obj;
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"self.results.count
:%ld", self.results.count);
[self.searchDisplayController.searchResultsTableView
reloadData];
});
}];
}
break;
case
2:
//搜公司
if ([[NSUserDefaults
standardUserDefaults]
objectForKey:@"accessToken"])
{
[[WBWeiboAPI
shareWeiboApi]
searchSuggestionsCompaniesWithString:self.mySearchbar.text
AndCount:20
CompletionCallBack:^(id
obj) {
self.results
= obj;
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"self.results.count
:%ld", self.results.count);
[self.searchDisplayController.searchResultsTableView
reloadData];
});
}];
}
break;
default:
break;
}
}
*)searchBar {
[self
searchWithString];
}
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController
*)controller {
NSLog(@"WillBeginSearch....");
}
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController
*)controller {
NSLog(@"DidBeginSearch....");
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController
*)controller {
NSLog(@"WillEndSearch....");
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController
*)controller {
NSLog(@"DidEndSearch....");
}
- (BOOL)searchDisplayController:(UISearchDisplayController
*)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self
searchWithString];
return
NO;
}
- (BOOL)searchDisplayController:(UISearchDisplayController
*)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self
searchWithString];
return
NO;
}
//因为这个控制器既是原来的WBDiscoverTableViewController。又是UISearchDisplayController的searchContentsController。
//WBDiscoverTableViewController的tableView和searchResultsTableView的delegat都指向这个对象(self)。
//所以须要在回调中差别究竟是哪个tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView {
if (tableView ==
self.tableView)
{
return
2;
} else
if (tableView ==
self.searchDisplayController.searchResultsTableView){
return
1;
} else
return
0;
}
- (NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView ==
self.tableView)
{
return
2;
} else
if (tableView ==
self.searchDisplayController.searchResultsTableView)
{
return
self.results.count;
} else
return
0;
}
- (UITableViewCell *)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView ==
self.tableView)
{
if (indexPath.section
== 0 && indexPath.row
== 0) {
return
self.hotTopicsCell1;
} else
if (indexPath.section
== 0 && indexPath.row
== 1) {
return
self.hotTopicsCell2;
} else
if (indexPath.section
== 1 && indexPath.row
== 0) {
return
self.nearbyPeopleCell;
} else {
return
self.nearbyWeiboCell;
}
} else
if (tableView ==
self.searchDisplayController.searchResultsTableView)
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"resultCell"];
id result =
self.results[indexPath.row];
if ([result
isMemberOfClass:[WBSearchSuggestionsOfUsers
class]]) {
WBSearchSuggestionsOfUsers * suggestion = result;
cell.textLabel.text
= suggestion.nickName;
cell.detailTextLabel.text
= suggestion.followersCount;
} else
if ([result
isMemberOfClass:[WBSearchSuggestionsOfSchools
class]]) {
WBSearchSuggestionsOfSchools *suggestion = result;
cell.textLabel.text
= suggestion.schoolName;
cell.detailTextLabel.text
= suggestion.location;
} else {
WBSearchSuggestionsOfCompanies *suggestion = result;
cell.textLabel.text
= suggestion.suggestion;
}
return cell;
} else
return
nil;
}
*)tableView heightForHeaderInSection:(NSInteger)section {
return
10;
}
- (void)tableView:(UITableView
*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIViewController *vc = [[UIViewController
alloc]init];
vc.view.backgroundColor
= [UIColor
whiteColor];
[self.navigationController
pushViewController:vc
animated:YES];
'*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]’。
检查了代码,并没有什么异常,最后想到是不是静态TableView导致的问题呢?
"WBDiscoverTableViewController.h"
@interface
WBDiscoverTableViewController ()
@property (weak,
nonatomic)
IBOutlet
UISearchBar *mySearchbar;
@property (nonatomic,
strong)
NSArray *results;
@end
@implementation WBDiscoverTableViewController
- (void)viewDidLoad {
[super
viewDidLoad];
registerNib:[UINib
nibWithNibName:@"WBsearchSuggestionCell"
bundle:[NSBundle
mainBundle]]
forCellReuseIdentifier:@"WBsearchSuggestionCell"];
[self.tableView
registerNib:[UINib
nibWithNibName:@"hotTopicsCell1"
bundle:[NSBundle
mainBundle]]
forCellReuseIdentifier:@"hotTopicsCell1"];
[self.tableView
registerNib:[UINib
nibWithNibName:@"hotTopicsCell2"
bundle:[NSBundle
mainBundle]]
forCellReuseIdentifier:@"hotTopicsCell2"];
[self.tableView
registerNib:[UINib
nibWithNibName:@"nearbyPeopleCell"
bundle:[NSBundle
mainBundle]]
forCellReuseIdentifier:@"nearbyPeopleCell"];
[self.tableView
registerNib:[UINib
nibWithNibName:@"nearbyWeiboCell"
bundle:[NSBundle
mainBundle]]
forCellReuseIdentifier:@"nearbyWeiboCell"];
}
{
switch (self.mySearchbar.selectedScopeButtonIndex)
{
case
0:
//搜用户
if ([[NSUserDefaults
standardUserDefaults]
objectForKey:@"accessToken"])
{
[[WBWeiboAPI
shareWeiboApi]
searchSuggestionsUsersWithString:self.mySearchbar.text
AndCount:20
CompletionCallBack:^(id
obj) {
self.results
= obj;
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"self.results.count
:%ld", self.results.count);
[self.searchDisplayController.searchResultsTableView
reloadData];
});
}];
}
break;
case
1:
//搜学校
if ([[NSUserDefaults
standardUserDefaults]
objectForKey:@"accessToken"])
{
[[WBWeiboAPI
shareWeiboApi]
searchSuggestionsSchoolsWithString:self.mySearchbar.text
AndCount:20
AndType:0
CompletionCallBack:^(id
obj) {
self.results
= obj;
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"self.results.count
:%ld", self.results.count);
[self.searchDisplayController.searchResultsTableView
reloadData];
});
}];
}
break;
case
2:
//搜公司
if ([[NSUserDefaults
standardUserDefaults]
objectForKey:@"accessToken"])
{
[[WBWeiboAPI
shareWeiboApi]
searchSuggestionsCompaniesWithString:self.mySearchbar.text
AndCount:20
CompletionCallBack:^(id
obj) {
self.results
= obj;
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"self.results.count
:%ld", self.results.count);
[self.searchDisplayController.searchResultsTableView
reloadData];
});
}];
}
break;
default:
break;
}
}
*)searchBar {
[self
searchWithString];
}
#pragma mark UISearchDisplayDelegate
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController
*)controller {
NSLog(@"WillBeginSearch....");
}
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController
*)controller {
NSLog(@"DidBeginSearch....");
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController
*)controller {
NSLog(@"WillEndSearch....");
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController
*)controller {
NSLog(@"DidEndSearch....");
}
- (BOOL)searchDisplayController:(UISearchDisplayController
*)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self
searchWithString];
return
NO;
}
- (BOOL)searchDisplayController:(UISearchDisplayController
*)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self
searchWithString];
return
NO;
#pragma mark - Table view data source
//因为这个控制器既是原来的WBDiscoverTableViewController,又是UISearchDisplayController的searchContentsController。
//WBDiscoverTableViewController的tableView和searchResultsTableView的delegat都指向这个对象(self)。
//所以须要在回调中差别究竟是哪个tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView {
if (tableView ==
self.tableView)
{
return
2;
} else
if (tableView ==
self.searchDisplayController.searchResultsTableView){
return
1;
} else
return
0;
}
- (NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView ==
self.tableView)
{
return
2;
} else
if (tableView ==
self.searchDisplayController.searchResultsTableView)
{
return
self.results.count;
} else
return
0;
}
- (UITableViewCell *)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView ==
self.tableView)
{
if (indexPath.section
== 0 && indexPath.row
== 0) {
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"hotTopicsCell1"
forIndexPath:indexPath];
return cell;
} else
if (indexPath.section
== 0 && indexPath.row
== 1) {
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"hotTopicsCell2"
forIndexPath:indexPath];
return cell;
} else
if (indexPath.section
== 1 && indexPath.row
== 0) {
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"nearbyPeopleCell"
forIndexPath:indexPath];
return cell;
} else {
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"nearbyWeiboCell"
forIndexPath:indexPath];
return cell;
}
} else
if (tableView ==
self.searchDisplayController.searchResultsTableView)
{
WBsearchSuggestionCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"WBsearchSuggestionCell"
forIndexPath:indexPath];
id result =
self.results[indexPath.row];
if ([result
isMemberOfClass:[WBSearchSuggestionsOfUsers
class]]) {
WBSearchSuggestionsOfUsers * suggestion = result;
cell.suggestion.text
= suggestion.nickName;
} else
if ([result
isMemberOfClass:[WBSearchSuggestionsOfSchools
class]]) {
WBSearchSuggestionsOfSchools *suggestion = result;
cell.suggestion.text
= suggestion.schoolName;
} else {
WBSearchSuggestionsOfCompanies *suggestion = result;
cell.suggestion.text
= suggestion.suggestion;
}
return cell;
} else
return
nil;
}
- (CGFloat)tableView:(UITableView
*)tableView heightForHeaderInSection:(NSInteger)section {
return
10;
}
- (void)tableView:(UITableView
*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath
animated:YES];
UIViewController *vc = [[UIViewController
alloc]init];
vc.view.backgroundColor
= [UIColor
whiteColor];
[self.navigationController
pushViewController:vc
animated:YES];
在storyboard中的静态UITableView中拖入 UISearchBar and Search Display Controller出现的奇怪问题的更多相关文章
- 在Storyboard中为UITableView添加Header和Footer
我在这里所说的Header和Footer并不是sectionHeader和sectionFooter,而是指UITableView的tableHeaderView和tableFooterView,这两 ...
- ios用storyboard快速创建静态cell
在实际开发中经常会遇到下面这样的页面,通常我们用静态cell来做可以快速创建,提高效率 下面讲一下用storyboard创建方法,将一个tableViewController控制器拖入storyboa ...
- ios UIScrolloView在storyboard中添加约束
1.在storyboard中如果有UINavigationbar 或 UITabar 布局的时候需要在控制器中勾选掉 Under Top Bars 和 Under Bottom Bars 这两个选项. ...
- WPF Prism MVVM 中 弹出新窗体. 放入用户控件
原文:WPF Prism MVVM 中 弹出新窗体. 放入用户控件 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_37214567/artic ...
- iOS中 xib自定义View在storyboard中的使用
1,创建UIView 的SubClass 命名为MyView 2, new一个名为MyView的xib p1 3,配置xib的属性 p2 4,为View 添加背景色,添加一个按钮并定制按钮约束,这里我 ...
- Django中使用静态资源/文件
Django中常需要引用js,css,小图像文件,一般我们把这一类文件称为静态文件,放置在static文件夹中,接下来,对Django中配置静态文件进行下傻瓜式的步骤介绍 在工程目录下新建static ...
- .NET静态代码织入——肉夹馍(Rougamo)
肉夹馍是什么 肉夹馍通过静态代码织入方式实现AOP的组件..NET常用的AOP有Castle DynamicProxy.AspectCore等,以上两种AOP组件都是通过运行时生成一个代理类执行AOP ...
- .NET静态代码织入——肉夹馍(Rougamo) 发布1.1.0
肉夹馍(https://github.com/inversionhourglass/Rougamo)通过静态代码织入方式实现AOP的组件,其主要特点是在编译时完成AOP代码织入,相比动态代理可以减少应 ...
- .NET静态代码织入——肉夹馍(Rougamo) 发布1.2.0
肉夹馍(https://github.com/inversionhourglass/Rougamo)通过静态代码织入方式实现AOP的组件,其主要特点是在编译时完成AOP代码织入,相比动态代理可以减少应 ...
随机推荐
- after I see Little Dorrit
也许是我太追名逐利,所以我不肯承认自己花费了大把的时间看电影,通过写博客好像自己从中感悟到了什么,好像看电影也是一种学习的方式. 也许是我平静自内心的方式,我太忙于玩或者学习,甚至没有机会非常沉静 一 ...
- S3C2440触摸屏控制总结
触摸屏控制原理,其实与ADC读取一个滑动变阻器中间触点电压的原理一样.只不过,读取触摸屏的X.Y方向上的电压需要两次,而且需要设置其工作模式以实现一个ADC读取两个通道的电压. S3C2440的ADC ...
- gulp 中的增量编译
最近花一点时间学了下 gulp,顺便学了下 sass,因为工作中并不需要用(我比较希望学习是需求驱动),所以一直拖到现在才学.突然觉得学习这类工具性价比很高,半天一天即可上手,技能树丰富了(尽管可能只 ...
- 前后端差异更小了——浅谈ES(ECMAScript)6
2015年6月,ES6正式发布.至今一年多的时间内,各个浏览器也对支持ES6做出了很大的改善,所以同学们无需顾忌你写代码的浏览器不认识~ 这么久的东西一定早有大神剖析过,今天我们以一个后端菜鸟的视角重 ...
- lua之mysql编程
环境搭建 #mysql 安装 sudo apt-get install mysql-server mysql-client # lua mysql 开发驱动安装 sudo apt-get instal ...
- Android使用SAX解析xml
一.理论准备 SAX采用事件驱动机制来解析XML文档,每当SAX解析器发现文档开始.元素开始.文本.元素结束.文档结束等事件时,就会向外发送一次事件,而开发者则可以通过编写事件监听器处理这些事 ...
- 制作PPT时,可能这些小习惯你需要注意
- Qt之自定义插件(for Qt Designer)
之前Blog里面有关于QWT的编译.配置.使用的文章,分别是在VS与Creator下进行的. QWT编译.配置.使用(VS2010 + Qt5.1.0). QWT编译.配置.使用(Qt Creator ...
- Highcharts实例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 17.1.1.4 Obtaining the Replication Master Binary Log Coordinates 得到复制master binary log 位置:
17.1.1.4 Obtaining the Replication Master Binary Log Coordinates 得到复制master binary log 位置: 你需要master ...