ios - 自动分页 PFQueryTableViewController

标签 ios objective-c parse-platform

我的目标是使用 PFQueryTableViewController 设置分页。我会对以下解决方案之一感到满意:

  • 1、表格 View 自动分页。例如,如果 objectsPerPage = 10,当实际行是第 10 行时,tableView 加载接下来的 10 个对象,依此类推。

  • 2,使用“加载更多单元格”UITableViewCell 进行分页。正如我所见,它是流行的解决方案,所以我也喜欢这种变体。

实际上,当我尝试第二种解决方案时,我没有收到任何错误,它只是行不通。我在 Storyboard上添加了一个新单元格并为其创建了一个类,并检查了 AnyPic 和其他相关代码作为起点。如我所见,当行数小于 objects.count 时,我应该显示 LoadMoreCell 单元格。我尝试了几种方法,但没有任何效果,反正我无法显示 LoadMoreCell

这是我的尝试:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row < self.objects.count) {
        NSLog(@"THE NUMBER OF ACTUAL ROW %d", indexPath.row);
        NSLog(@"cell tapped");

    } else  { 

        // load more
        [self loadNextPage];
        NSLog(@"LOAD NEXT PAGE");

    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *LoadMoreCellIdentifier = @"loadmore";
    LoadMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LoadMoreCellIdentifier];

    if (!cell) {
        cell = [[LoadMoreTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LoadMoreCellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.loadMoreLabel.text = @"LOAD MORE CELLS";
        NSLog(@"NO CELL");
          }

    return cell;
}

我实现的另一部分是问题的最后代码部分。

我最初的问题涵盖了我对第一个(自动分页)变体的尝试。

我原来的问题:

我有一个可用的 PFQueryTableViewController,但我无法为其设置分页。我的计划很简单,我想在用户滚动到 TableView 底部时加载下一页。如果表格显示 10 个项目/页面,我想在 10. 单元格出现时显示下一个 10 项目。

实际上,我正在检测用户何时到达表格底部,然后为新项目调用 [self loadNextPage],但应用程序崩溃并出现此错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 10 from section 0 which only contains 10 rows before the update'

这个问题我不是很懂,因为datasource(一个数组)有40多个对象可以加载。

下面是我如何管理方法调用(在 initWithCoder 启用分页)

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;
    float reload_distance = 10;
    if(y > h + reload_distance) {
        NSLog(@"load more rows");
        [self loadNextPageInTable];
    }
}

-(void) loadNextPageInTable {

     [self loadNextPage];
    NSLog(@"NEW PAGE LOADED");
}

这是表格 View 设置

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.parseClassName = @"followClass";
        self.pullToRefreshEnabled = YES;
        self.paginationEnabled = YES;
        self.objectsPerPage = 10;
    }
    return self;
}


- (PFQuery *)queryForTable {
    if (![PFUser currentUser]) {
        return nil;
    }

    PFQuery *followQuery = [PFQuery queryWithClassName:@"self.parseClassName"];
    [followQuery whereKey:@"followed" equalTo:[PFUser currentUser]];

    return query;

}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {


    static NSString *CellIdentifier = @"followCell";
    FeedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.followLabel.text = object[@"username"];
    cell.avatar.file = object[@"image"];
    [cell.avatar loadInBackground];

    return cell;

}

最佳答案

这是在我的测试项目中运行的代码。当 numberOfRowsInSection 没有被注释掉时,我确实得到了你得到的错误。我确实看到加载更多单元格,但它会自动加载其余单元格让我知道是否有帮助

#import "ParseDataTableViewController.h"

@implementation ParseDataTableViewController


- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Custom the table

        // The className to query on
        //self.parseClassName = @"ParseTest";

        // The key of the PFObject to display in the label of the default cell style

        // The title for this table in the Navigation Controller.
        self.title = @"Test";

        // Whether the built-in pull-to-refresh is enabled
        self.pullToRefreshEnabled = YES;

        // Whether the built-in pagination is enabled
        self.paginationEnabled = YES;

        // The number of objects to show per page
        self.objectsPerPage = 15;
    }
    return self;
}




- (IBAction)nextPage:(id)sender
{
    [self loadNextPageInTable];
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;
    float reload_distance = 15;
    if(y > h + reload_distance) {
        NSLog(@"load more rows");
        [self loadNextPageInTable];
    }
}

-(void) loadNextPageInTable {

    [self loadNextPage];
    NSLog(@"NEW PAGE LOADED");
}


- (PFQuery *)queryForTable {


    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name BEGINSWITH 't'"];
    PFQuery *followQuery = [PFQuery queryWithClassName:@"ParseTest" predicate:predicate];


    return followQuery;

}

/*-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.objects.count;
}*/



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {


    static NSString *CellIdentifier = @"DataCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.textLabel.text = object[@"Name"];

    return cell;

}



@end

关于ios - 自动分页 PFQueryTableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27253685/

相关文章:

ios - 如何从相机胶卷和照片库中选择多个视频?

iphone - 从 plist 数组加载 MapView 注释时出现问题 - Xcode 4.2

objective-c - 滚动 UItableView 并回击时应用程序崩溃

android - 在Android中使用parse SDK更改parse用户的密码

ios - iOS 版 Parse 中的 OR 查询

ios - Pod 规范中的 Cocoapods 依赖项不起作用

ios - 是否可以通过应用内购买购买新应用?

iphone - ScheduleLocalNotification 不适用于越狱应用程序?

objective-c - 如何按升序对 NSMutablearray 进行排序

javascript - 如何使用 REST API 将 Parse.com 中的日期列设置为 NULL?