自动分页PFQueryTableViewController

rih*_*ihe 7 objective-c ios parse-platform

我的目标是用a来设置分页PFQueryTableViewController.我对其中一个解决方案感到满意:

  • 1,自动分页表视图.例如,如果objectsPerPage = 10tableView实际行为10行时加载下10个对象,依此类推.

  • 2,使用"加载更多单元格"进行分页UITableViewCell.我认为它是流行的解决方案,所以我也很喜欢这种变化.

实际上,当我尝试第二种解决方案时,我没有遇到任何错误,它只是无法正常工作.我在Storyboard上添加了一个新单元格,并为它创建了一个类,并检查了AnyPic和其他相关代码作为起点.正如我所见,LoadMoreCell当行数小于时,我应该显示单元格objects.count.我尝试了几种方法,但没有任何作用,我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;
}
Run Code Online (Sandbox Code Playgroud)

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

我的原始问题涵盖了我对第一个(自动分页)变体的尝试.

我原来的问题:

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

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

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'尝试从更新前仅包含10行的第0部分删除第10行'

我真的不明白这个问题,因为数据源(一个数组)有超过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");
}
Run Code Online (Sandbox Code Playgroud)

这是表视图设置

- (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;

}
Run Code Online (Sandbox Code Playgroud)

Yan*_*Yan 3

这是在我的测试项目中有效的代码。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
Run Code Online (Sandbox Code Playgroud)