3

Я хочу добавить дополнительную ячейку в UICollectionview? может кто-нибудь сказать мне, как я могу добавить кнопку загрузки? я создал CollectionView, который работает хорошо, но я хочу, чтобы добавить Load больше кнопки в нижней части обзора коллекции клетки, как thisКак добавить дополнительную ячейку в UICollectionView?

Вот мой взгляд коллекция

#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

ответ

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; 
} 
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 всегда добавьте в конец своего вида коллекции.

Надеюсь, это поможет!

Смежные вопросы