2

我正在尝试将搜索添加到Todo 演示应用程序。到目前为止,我有以下代码,搜索工作。以下代码的问题是分页不再起作用。有关于分页的类似问题,但是当“节中的行数 + 1”返回时,应用程序崩溃并出现 [__NSArrayM objectAtIndex:] 错误。如何让分页工作?

// 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
4

1 回答 1

0

在您自己的实现中,numberOfRowsInSection您必须返回 numberOfRows + 1。但是考虑到您有比查询返回更多的单元格,您必须编写代码。查看您 heightForRowAtIndexPath和其他方法:可以调用 [self.objects objectAtIndex:self.objects.count]。我的意思是,如果indexPath.row == self.objects.count,它是一个“加载更多”单元格。如果您覆盖willSelectRowAtIndexPathor didSelectRowAtIndexPath,则必须在方法的顶部执行此操作

 [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:

于 2013-04-19T10:02:51.523 回答