2012-11-04 34 views
2

我試圖將搜索添加到Todo demo應用程序。到目前爲止,我的搜索工作中有以下代碼。下面的代碼的問題是,分頁不再工作。關於pagination有一個類似的問題,但是當「section + 1中的行數」返回時,應用程序會崩潰,並顯示[__NSArrayM objectAtIndex:]錯誤。我如何獲得分頁工作?Parse.com - 如何獲得PFQueryTableViewController,分頁與UISearchDisplayController一起工作?

// MyTableController.h

#import <Parse/Parse.h> 

@interface MyTableController : PFQueryTableViewController 

@end 

// MyTableController.m

#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 = @"Todo"; 


     self.keyToDisplay = @"text"; 


     self.pullToRefreshEnabled = YES; 


     self.paginationEnabled = YES; 


     self.objectsPerPage = 5; 
    } 
    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:@"text" containsString:searchTerm]; 

    NSArray *results = [query findObjects]; 

    [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 ; 
    } 
} 


#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:@"priority"]; 

    return query; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object { 
static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = [object objectForKey:@"text"]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Priority: %@", [object objectForKey:@"priority"]]; 

    return cell; 
} 




#pragma mark - Table view delegate 

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


@end 

回答

0

在自己實現numberOfRowsInSection必須返回numberOfRows + 1,但你必須編寫代碼考慮到你比查詢返回更多的單元格。檢查你的heightForRowAtIndexPath和其他方法:可以調用[self.objects objectAtIndex:self.objects.count]。我的意思是,如果indexPath.row == self.objects.count,它的一個「加載更多」單元格。 如果重寫willSelectRowAtIndexPathdidSelectRowAtIndexPath,你必須在方法

[super tableView:tableView didSelectRowAtIndexPath:indexPath]; 

的頂部做這個或添加這個(以前的情況是首選)

if (indexPath.row == self.objects.count) 
{ 
    [self loadNextPage]; 
} 

定製載入更多細胞放置在-(PFTableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath

而我使用 -(PFTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath 而不是 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:

相關問題