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