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

独自の実装では、numberOfRowsInSectionnumberOfRows + 1 を返す必要があります。ただし、クエリが返すよりも多くのセルがあることを考慮して、コードを記述する必要があります。あなたと他のメソッドをチェックしてください heightForRowAtIndexPath: [self.objects objectAtIndex:self.objects.count] への呼び出しがあるかもしれません。つまりindexPath.row == self.objects.count、「もっと読み込む」セルです。willSelectRowAtIndexPathまたはをオーバーライドする場合didSelectRowAtIndexPathは、メソッドの先頭でこれを行う必要があります

 [super tableView:tableView didSelectRowAtIndexPath:indexPath];

またはこれを追加します(前のケースが優先されます)

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

配置された Load More セルのカスタマイズ-(PFTableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath

そして、 -(PFTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath 代わりに 使用しています- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:

于 2013-04-19T10:02:51.523 に答える