kzia kzia - 9 months ago 36
iOS Question

Loading additional cells at the bottom of UICollectionView

I am trying to automatically get and load the next page of cells when the user scrolls to the bottom of the

UICollectionView
.

I am able to do so by adding a
UIButton
in
UICollectoinView
footer. When user scrolls to the footer and touches the button, new cells are added correctly.
UIButton
in the footer can be touched again for adding more pages.

I tried to automate it so when the user scrolls to the bottom, the proper method for adding new cells is called automatically:

- (UICollectionReuseableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElementOfKind: (NSString *)kind atIndexPath: (NSIndexPath *) indexPath
{

// code for headerView
// ...

// code for footerView
MySupplementalFooter *footerView = nil;
if ( [kind isEqual:UICollectionElementKindSectionFooter] )
{
footerView = [self.collectionView dequeueReuseableSupplemntaryViewOfKind:kind withReuseIdentifier:@"FooterId" forIndexPath:indexPath];
if (LoadMoreIsEnabled)
{
footerView.loadMoreFooterButton.setHidden:NO];
[self loadMoreFooterButtonAction]; // calls the method to add cells to the dictionary for cv
} else
{
footerView.loadMoreFooterButton setHidden:YES];
}

return footerView;
}
}


The
loadMoreFooterButtonAction
method is called, but the entire
UICollectionView
blanks out. Refreshing the
UICollectionView
does not bring it back.

Answer Source

I'm assuming that you're trying to do something like "infinite scrolling" from desktop webpages, where you load more content when a user gets to the bottom of a page. On iOS, this isn't a very commonly used design. Typically, developers will initialize their UITableView or UICollectionView with the absolute total number of entries. If they need to stream data over the network, they asynchronously load the data as needed (when cells or views come on screen) and until the content loads they show some kind of temporary content, like a blank view with a spinner.

That said, if you want to do infinite scrolling in a manner more like a desktop web app, you should implement the UIScrollViewDelegate protocol (or UICollectionViewDelegate, which is a superset) and listen for the scrollViewDidScroll: callback. Within that callback, iterate through the UICollectionView's indexPathsForVisibleItems and check to see if any of the index paths map to the 'last' item in your collection, which indicates the user has scrolled to the end. At that point, call your logic to load more stuff.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download