Pfquery TableView与搜索栏崩溃

时间:2014-12-11 19:59:59

标签: ios parse-platform pfquery

我正在基于Todo parse.com教程应用添加搜索栏到应用程序。搜索工作正常,但是当我删除搜索字词时,应用程序崩溃了。因此,例如,我输入一个首字母缩略词“ABC”,它会使用searchm返回值,因为我删除了“ABC”应用程序崩溃时出现了越界错误。

#import "MyTableController.h"

@interface MyTableController() <UISearchDisplayDelegate> {
    
}

@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResults;

@end

@implementation MyTableController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
       
        self.className = @"Jargon";
        
       
        self.keyToDisplay = @"acronym";
        
       
        self.pullToRefreshEnabled = YES;
        
       
        self.paginationEnabled = YES;
        
       
        self.objectsPerPage = 2;
    }
    return self;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    self.tableView.tableHeaderView = self.searchBar;
    
    
    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar
                                                              contentsController:self];
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
    self.searchController.delegate = self;
    
    CGPoint offset = CGPointMake(0, self.searchBar.frame.size.height);
    self.tableView.contentOffset = offset;
    
     self.searchResults = [NSMutableArray array];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];

}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning
{
 
    [super didReceiveMemoryWarning];
    
 
}

- (void)filterResults:(NSString *)searchTerm {
    [self.searchResults removeAllObjects];
    
     PFQuery *query = [PFQuery queryWithClassName: self.className];
    
    [query whereKey:@"acronym" containsString:searchTerm];
    
    NSArray *results  = [query findObjects:nil];
    
    
    
    [self.searchResults addObjectsFromArray:results];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.tableView) {
        return self.objects.count ;
    } else {
        return self.searchResults.count +1;
    }
}


#pragma mark - Parse

- (void)objectsDidLoad:(NSError *)error {
    [super objectsDidLoad:error];
}

- (void)objectsWillLoad {
    [super objectsWillLoad];
    
}


- (PFQuery *)queryForTable {
    
    PFQuery *query = [PFQuery queryWithClassName:self.className];
    if ([self.objects count] == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }
 
    [query orderByAscending:@"acronym"];
 
    return query;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
 static NSString *CellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
 
    cell.textLabel.text = [object objectForKey:@"acronym"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Term: %@", [object objectForKey:@"text"]];
 
    return cell;
}





#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
}


@end

0 个答案:

没有答案