Parse Pagination loadNextPage第二页加载两次

时间:2015-02-14 05:10:15

标签: ios parse-platform pagination

在我的Parse应用程序中,我启用了Pagination,并且出于测试目的,每页的对象设置为5.当我运行应用程序时,我在TableView中得到它

  

1

     

2

     

3

     

4

     

5

     

加载更多

单击“加载更多”后,整个表格如下所示:

  

1

     

2

     

3

     

4

     

5

     

6

     

7

     

8

     

9

     

10

     

6

     

7

     

8

     

9

     

10

在此之后单击“加载更多”将两次添加11-15的集合。发生了什么事?

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

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

        // 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 = 5;

    }
    return self;
}
- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:@"Prayers"];

    // If no objects are loaded in memory, we look to the cache first to fill the table
    // and then subsequently do a query against the network.
    if (self.objects.count == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    [query orderByDescending:@"createdAt"];

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

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




    self.theObject = object;
    BOOL anony = [object[@"Anonymous"] boolValue];


    cell.profileName.text = object[@"Title"];
    cell.contentLabel.text = object[@"Request"];
    cell.firstName = object[@"FirstName"];
    cell.lastName = object[@"LastName"];
    cell.iostoken = object[@"DeviceID"];
    cell.request = object[@"Title"];
    cell.prayerObject = object;
    PFFile *thumbnail = object[@"ProfilePic"];
    cell.profilePic.image = [UIImage imageNamed:@"AppIcon60x60@2x.png"];
    /*[cell.commentButton addTarget:self action:@selector(didTapCommentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.commentButton setTitle:@"Share" forState:UIControlStateNormal];
    [cell.commentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cell.commentButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];*/
    [thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {

        UIImage *thumbnailImage = [UIImage imageWithData:imageData];
        UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:thumbnailImage];

        cell.profilePic.image = thumbnailImage;

    }];
    NSString *dates = object[@"dateMade"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMM_dd_yyyy"];
   NSDate *datefromstring = [formatter dateFromString:dates];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"MMM dd, yyyy"];
    cell.dateLabel.text = [formatter2 stringFromDate:datefromstring];
    UIFont *cellFont = [UIFont fontWithName:@"Verdana-Bold" size:15];
    UIFont *cellFont2 = [UIFont fontWithName:@"Verdana-Bold" size:12];



    return cell;
}
- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.objects.count) {
        return nil;
    } else {
        return [super objectAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    PFObject *object = [self objectAtIndexPath:indexPath];

    if (object == nil) {
        // Return a fixed height for the extra ("Load more") row
        return 70;
    } else {



   NSLog(@"%lu",  (unsigned long)[self.objects count]);
     PFObject *entry = [self.objects objectAtIndex:indexPath.row];
    NSString *commentString = entry[@"Request"];
    NSString *nameString = @"";
    NSLog(@"%@", commentString);
return [Cell heightForCellWithContentString:(NSString *)commentString] +25 ;
    }
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [super tableView:tableView didSelectRowAtIndexPath:indexPath];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.row == self.objects.count && self.paginationEnabled) {
        // Load More Cell
        NSLog(@"Load More");
        [self loadNextPage];
    }

1 个答案:

答案 0 :(得分:1)

-(PFQuery *)queryForTable {

...

...

...

//Always trigger a network request.

[tableQuery setCachePolicy:kPFCachePolicyNetworkOnly];

//If no objects are loaded in memory, we look to the cache first to fill the table and then subsequently do a query against the network.

if(self.objects.count == 0) {

   [tableQuery setCachePolicy: kPFCachePolicyCacheThenNetwork];

   }

 ...

 ...

 ...

 return tableQuery;

}