My SearchDisplayController doesn't work well w

2019-08-17 04:27发布

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

0条回答
登录 后发表回答