gpt4 book ai didi

ios - 解析分页 loadNextPage 第二页加载两次

转载 作者:行者123 更新时间:2023-11-29 12:27:59 24 4
gpt4 key购买 nike

在我的 Parse 应用程序中,我启用了分页功能,出于测试目的,每页的对象设置为 5。当我运行该应用程序时,我在 TableView 中得到了这个

1

2

3

4

5

Load More

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

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];
}

最佳答案

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

}

关于ios - 解析分页 loadNextPage 第二页加载两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28512644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com