Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 theUICollectionView.

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.

like image 225
kzia Avatar asked Jun 18 '13 23:06

kzia


People also ask

Do CollectionView use cells?

Like a table view, a collection view is a UIScrollView subclass. UICollectionViewCell: This is similar to a UITableViewCell in a table view. These cells make up the view's content and are subviews to the collection view. You can create cells programmatically or inside Interface Builder.

How do I add a section title in UICollectionView?

There are no section headers in the UICollectionView. So for your first task, you'll add a new section header using the search text as the section title. To display this section header, you'll use UICollectionReusableView .

How do I select an item in CollectionView?

Single selectionWhen the SelectionMode property is set to Single , a single item in the CollectionView can be selected. When an item is selected, the SelectedItem property will be set to the value of the selected item.

What is the use of datasource and delegate in UICollectionView in IOS?

This method asks the data source object to provide a supplementary view to display in the collection view. It asks the datasource object whether the specified item can be moved to another location in the collectionview. This method moves the specified item to the specified indexpath.


2 Answers

In IOS8 there is a UICollectionViewDelegate method willDisplayCell.

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    if indexPath.item == (data.count-1) {
        loadData()
    }
}
like image 97
Nick Wargnier Avatar answered Oct 11 '22 00:10

Nick Wargnier


In table view, you should load more data in willDisplayCell() method. With collections, there is no such a counterpart. But, you can do like this. Hope, it helps.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // Other code here ...

    if (indexPath.item == [self.photos count] - 1) {
        [self loadPhotos];
    }

    return cell;
}

- (void)loadPhotos {
    [self showLoadingIndicator];
    self.operation = [client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hideLoadingIndicator];

        // Get data
        NSArray *photosData = responseObject[@"data"];
        NSInteger numberOfResults = [photosData count];
        NSInteger totalCount = [self.photos count];

        if (numberOfResults > 0) {
            NSMutableArray *indexPaths = [NSMutableArray new];

            for (NSInteger i = 0; i < numberOfResults; i++) {
                Photo *photo = [Photo new];
                photo._id = [photoData[@"id"] integerValue];
                photo.url = photoData[@"url"];

                [self.photos addObject:photo];
                [indexPaths addObject:[NSIndexPath indexPathForRow:totalCount + i
                                                     inSection:0]];
            }

            [self.collectionView performBatchUpdates:^{
                [self.collectionView insertItemsAtIndexPaths:indexPaths];
            } completion:nil];
        }        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hideLoadingIndicator];
        // Handle error here
    }];

   [self.operation start];
}
like image 42
Sukhrob Avatar answered Oct 10 '22 23:10

Sukhrob