Search code examples
iosobjective-ccollectionview

Objective-C - How to add static first cell in UICollectionview?


I want to make the first cell fixed.

So, I made 2 section and write like this

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if(section == 0){
        return 1;
    }else{
        return self.assetsFetchResults.count;
    }

}

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


#pragma mark - UICollectionViewDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSInteger currentTag = cell.tag + 1;
//    
//    cell.tag = currentTag;
//    NSLog(@"cell tag %d" , cell.tag);

    if(indexPath.section == 0){
        PhotoLibraryFirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoLibraryFirstCell" forIndexPath:indexPath];
        return cell;
    }else{
        PhotoLibraryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoLibraryCell" forIndexPath:indexPath];
        PHAsset *asset = self.assetsFetchResults[indexPath.item];
        [self.imageManager requestImageForAsset:asset
                                     targetSize:AssetGridThumbnailSize
                                    contentMode:PHImageContentModeAspectFill
                                        options:nil
                                  resultHandler:^(UIImage *result, NSDictionary *info) {
//                                      cell.imageView.image = result;
                                  }];
        return cell;
    }

But if I write like this

Space is created between 0 section and 1 section like this image.

How can I attach section 0 and section 1?

enter image description here


Solution

  • Don't try to add another section in collection view , instead use the same section. Keep the first row static by the following check I made in the code.

     - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
             return self.assetsFetchResults.count+1;
    
        }
    
        - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
            return 1;
        }
    
    
        #pragma mark - UICollectionViewDelegate
        - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        //    NSInteger currentTag = cell.tag + 1;
        //    
        //    cell.tag = currentTag;
        //    NSLog(@"cell tag %d" , cell.tag);
    
            if(indexPath.row == 0){
                PhotoLibraryFirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoLibraryFirstCell" forIndexPath:indexPath];
                return cell;
            }else{
                PhotoLibraryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoLibraryCell" forIndexPath:indexPath];
                PHAsset *asset = self.assetsFetchResults[indexPath.item-1];
                [self.imageManager requestImageForAsset:asset
                                             targetSize:AssetGridThumbnailSize
                                            contentMode:PHImageContentModeAspectFill
                                                options:nil
                                          resultHandler:^(UIImage *result, NSDictionary *info) {
        //                                      cell.imageView.image = result;
                                          }];
                return cell;
            }