0

Here I have a Search Bar on the top of the UIViewController, and also a UITableView below the Search Bar. When I don't Search things, the tableView could display things well, but when I search, the searchResultTableView can only show some white cell, not the correct result.

Here is the .h file:

#import <UIKit/UIKit.h>

@interface MySearchViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;


- (void)reloadView;

@end

And here is the .m file:

#import "MySearchViewController.h"
#import "MyEventInfo.h"
#import "MyEventTableViewCell.h"
#import "MyEventDetailViewController.h"

@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 (NSData *dataObject in dataArray) {
        MyEventInfo *eventDecodedObject = [NSKeyedUnarchiver unarchiveObjectWithData:dataObject];
        [events addObject:eventDecodedObject];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.myTableView) {
        return [events count];
    } else {
        NSLog(@"searchResults count:%lu",(unsigned long)[searchResults count]);
        return [searchResults 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.myTableView) {
        event = [events objectAtIndex:indexPath.row];
    } else {
        event = [searchResults objectAtIndex:indexPath.row];
        NSLog(@"name of event:%@", event.nameOfEvent);
    }

    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
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"nameOfEvent contains[c] %@", searchText];
    searchResults = [events filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString];
    return YES;
}

@end

And on the storyboard I've already set the tableView's delegate and datasource to self. This drives me crazy, I cannot figure out what's wrong. Could someone help me out? Thanks.

when searching, cell's objects were all null, and had nothing in itself

when not searching, the cell is well with objects that had content the same as event

  • Have tried putting a break point in the `searchDisplayController:shouldReloadTableForSearchString:` method? Does this method get called? – Adeel Miraj Oct 16 '15 at 04:55
  • yes, it is get called, and I said I NSLogged the filtered things out, they were correct, but in the searchDisplay, there were only blank custom cells, so I think there isn't something wrong with filter. – Michael GUO Oct 16 '15 at 07:55
  • I added some break point and I found some problems. I attached a screenshot. the custom cell is always null, even after assignment from event object. – Michael GUO Oct 16 '15 at 08:09
  • I found that when searching, the objects in cell are null, and with nothing in it, when not searching, the cell is well, and its label has content, which is correct with event's, I was wondering what was wrong with the cell when searching, why its object were null. – Michael GUO Oct 16 '15 at 08:23
  • Have a look at this thread. http://stackoverflow.com/questions/15892651/uisearchdisplaycontroller-not-correctly-displaying-custom-cells – Adeel Miraj Oct 16 '15 at 08:27
  • Thanks, 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];" – Michael GUO Oct 16 '15 at 08:44

0 Answers0