【问题标题】:Objective-C - How to add static first cell in UICollectionview?Objective-C - 如何在 UICollectionview 中添加静态第一个单元格?
【发布时间】:2018-07-12 06:51:49
【问题描述】:

我想修复第一个单元格。

所以,我做了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;
    }

但是如果我这样写

在 0 节和 1 节之间创建空间,就像这张图片一样。

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

【问题讨论】:

  • 在我看来,您应该将所有单元格放在一个部分中。而不是检查indexPath.section == 0,你应该检查indexPath.item == 0

标签: ios objective-c collectionview


【解决方案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;
        }

【讨论】:

    猜你喜欢
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多