Here I have a ViewController, with a search bar on the top and a tableView below it, and what I expected is the tableView show all the things when no searching, then if there is some searching, the searching's tableview will display the search result. And I've implemented it, but there is something wrong, which is the tableView could show things correctly without searching, but when I made searching, the table view below the search bar showed nothing, and I NSLog out the rowInSection, and find it is correct, but why there is nothing showed up? Here is the code, could somebody go over and find something wrong? Thanks in advance. (I did the same thing in a TableViewController, it worked, but I switched into ViewController, it didn't work).
//MySearchViewController.h
@interface MySearchViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
- (void)reloadView;
@end
//MySearchViewController.m
@interface MySearchViewController ()
@property NSUserDefaults *usrDefault;
@end
@implementation MySearchViewController {
NSMutableArray *events;
NSArray *searchResults;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.usrDefault = [NSUserDefaults standardUserDefaults];
events = [[NSMutableArray alloc] init];
[self extractEventArrayData];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[self reloadView];
}
- (void)reloadView {
NSLog(@"reloadView");
events = [[NSMutableArray alloc] init];
[self extractEventArrayData];
[self.myTableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)extractEventArrayData {
NSArray *dataArray = [[NSArray alloc] initWithArray:[self.usrDefault objectForKey:@"eventDataArray"]];
for (NSDate *dataObject in dataArray) {
MyEventInfo *eventDecodedObject = [NSKeyedUnarchiver unarchiveObjectWithData:dataObject];
[events addObject:eventDecodedObject];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(@"searchResults count:%lu",(unsigned long)[searchResults count]);
return [searchResults count];
} else {
return [events count];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 360;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomTableCell";
MyEventTableViewCell *cell = (MyEventTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[MyEventTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Display MyEventTableViewCell in the table cell
MyEventInfo *event = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
event = [searchResults objectAtIndex:indexPath.row];
} else {
event = [events objectAtIndex:indexPath.row];
}
cell.nameOfEvent.text = event.nameOfEvent;
cell.imageOfEvent.image = [UIImage imageNamed:event.imageOfEvent];
cell.timeOfEvent.text = event.timeOfEvent;
cell.locationOfEvent.text = event.locationOfEvent;
cell.dateOfEvent.text = event.dateOfEvent;
return cell;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"nameOfEvent contains[c] %@", searchText];
searchResults = [events filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}