0

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;
}
  • Did you remember to set the tableview.delegate and tableView.dataSource to self? – scord Oct 15 '15 at 22:07
  • Yes, I did set that, and the tableview could display correctly, but when I wanna do some search, the tableview of search controller could not display things correctly. – Michael GUO Oct 15 '15 at 22:16
  • Did you set up a UISearchDisplayController to go with your search bar? Is your search bar a subview of your tableview? – beyowulf Oct 16 '15 at 02:36
  • yes, I posted a same question here, which might be more specific.http://stackoverflow.com/questions/33162041/my-searchdisplaycontroller-doesnt-work-well-with-uitableview-in-uiviewcontrolle – Michael GUO Oct 16 '15 at 03:39

2 Answers2

0

First off, UISearchDisplayController was deprecated in iOS 8. You should be using UISearchController. I believe you are using the UISearchDisplayController because you set it up in Storyboard and Apple hasn't updated Storyboard's Object Library to use UISearchController. BUT, you should still convert to UISearchController and implement everything in code until Apple updates the Storyboard Object Library. It is quite simple to set up in code.

Anyway to your current issue, I believe it is resulting from the fact that you are not setting the datasource and delegate of the searchDisplayController's searchResultsTableView.

In viewDidLoad, you should do

searchDisplayController.searchResultsTableView.delegate = self;
searchDisplayController.searchResultsTableView.dataSource = self;

Simply setting the delegate and the dataSource of the IBOutlet "myTableView" will not do the trick because once you start searching, the searchDisplayController presents its own tableView "searchDisplayController.searchResultsTableView" on top of the current view. The current view controller needs to be the dataSource and delegate of searchResultsTableView.

Hope this works/helps!

mattyb
  • 1,011
  • 2
  • 10
  • 26
  • Hi there, thanks for your answer, but I've already tried what you said, it didn't work. The problem I encountered is that when I do searching, there did come out a resultTableView, but this new tableView contains only blank cell!, and in console I NSLog the number of row, it looked correct to what it should be. I kind of cannot figure out why the cell is blank. Could you please take a look at the picture I updated in my question. Thank you very much. – Michael GUO Oct 16 '15 at 05:50
  • Glad you figured it out! – mattyb Oct 16 '15 at 20:41
0

Thanks everyone, I figured it out, cuz I wrote

MyEventTableViewCell *cell = (MyEventTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

but it should be like this

MyEventTableViewCell *cell = (MyEventTableViewCell *)[self.myTableView dequeueReusableCellWithIdentifier:CellIdentifier];

what a mistake!