How to add Load more cell in UICollectionView?

时间:2015-06-26 09:42:36

标签: ios objective-c uicollectionview uicollectionviewlayout

I want to add load more cell in UICollectionview ? can anyone tell me how can i add load button ? i have created collectionview that works fine but i want to add Load more button in bottom of collection view cell like this

Here's my collection view

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
      return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
     switch (section) {
            case 0: return 66;
            case 1: return 123;
     }
     return 31;
 }

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

   NSUInteger row = [indexPath row];
   NSUInteger count = [self.stockImages count];

   if (row == count) {

        //Add load more cell
   }

  DemoCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[DemoCellView reuseIdentifier] forIndexPath:indexPath];

  // Configure the cell
  cell.titleLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.item + 1];
  NSLog(@"%@", self.stockImages[indexPath.item % self.stockImages.count]);
  cell.imageView.image = self.stockImages[indexPath.item % self.stockImages.count];

  return cell;
}


#pragma mark <DemoLayoutDelegate>

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

     // Do something 
     // Insert new cell after clicking load more data 

}

 - (CGFloat)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout *)collectionViewLayout heightForHeaderInSection:(NSInteger)section {
     return kFMHeaderFooterHeight;
}

  - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForFooterInSection:(NSInteger)section {
    return kFMHeaderFooterHeight;
 }

enter image description here

2 个答案:

答案 0 :(得分:8)

您可以添加页脚

- (UICollectionReusableView *)collectionView:(JSQMessagesCollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {

        //load your footer you have registered earlier for load more 
        [super dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                                                                             withReuseIdentifier:@“load more footer”
                                                                                    forIndexPath:indexPath];
    }

    return nil;
}

答案 1 :(得分:0)

cellForItemAtIndexPath方法中,您可以查看:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ImageCollectionViewCell *cellImage;
    AddMoreCollectionViewCell *addMoreCell;
    if(indexPath.row < [_dataSource numberOfItems]){
        cellImage = [collectionView dequeueReusableCellWithReuseIdentifier:ImageCollectionCellIdentifier
                                                              forIndexPath:indexPath];
        [cellImage configureForMediaViewModel:[_dataSource mediaViewModelForItemIndex:indexPath.row] delegate:self];
        return cellImage;
    }else{
        addMoreCell = [collectionView dequeueReusableCellWithReuseIdentifier:AddMoreCollectionCellIdentifier
                                                                forIndexPath:indexPath];
        addMoreCell.delegate = self;
        return addMoreCell;

    }
}

其中ImageCollectionViewCell是主要的单元格类型,AddMoreCollectionViewCell是带有加号('+')符号和其他内容的单元格。

使用此方法,AddMoreCollectionViewCell始终在集合视图的末尾添加。

希望它有所帮助!