My goal is to configure pagination using the PFQueryTableViewController
. I would be pleased with one of these solutions:
1, Paging a table view automatically. As with objectsPerPage = 10
tableView
loads the next 10 objects when the actual row is row 10. etc.
2, paging with "load more cell" UITableViewCell
. As I see it, this is a popular solution, so I will also like this option.
Actually, I am not getting errors when trying the second solution, it just just doesn't work. I added a new cell on the Storyboard and created a class for it, and checked AnyPic and other related codes as a starting point. As I can see, I should display the LoadMoreCell
cell when the number of rows is less than objects.count
. I tried several ways, but nothing works, I still can not display LoadMoreCell
.
Here is my attempt:
- (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 {
Another part of my implementation is the last part of the question code.
My initial question is about my first attempts (paging automatically).
My original question is:
I have a working PFQueryTableViewController
, but I can not configure pagination for it. My plan is simple, I want to load the next page when the user scrolls to the bottom of the table. If the table displays 10 elements / pages, I would like to display the next 10 elements when cell 10 appears.
In fact, I detect when the user has reached the bottom of the table, and then calls [self loadNextPage]
for new elements, but the application crashes with this error:
Application termination due to the uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete line 10 from section 0, which contains only 10 lines before updating'
I really do not understand this problem, because the data source (array) contains more than 40 objects that can be loaded.
Here's how I control the method call (pagination is included in 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"); }
And here is the table view setup
- (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; }