Objective-C - 如何在 UICollectionview 中添加静态第一个单元格?

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

我想固定第一个单元格。

所以,我做了 2 个部分,然后这样写

- (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;
    }

但是如果我这样写

Space 是在第 0 个部分和第 1 个部分之间创建的,就像这张图片。

如何附加第 0 节和第 1 节?

不要尝试在集合视图中添加其他部分,而是使用相同的部分。通过我在代码中进行的以下检查,使第一行保持静态。

 - (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;
        }