ios - CollectionView 与单元格重叠

标签 ios objective-c cocoa-touch uikit uicollectionview

我的目标是在我的 View 底部有一个 Collection View 。这些单元格中充满了照片库中的照片。我不知道为什么,但细胞相互重叠。有人知道为什么吗?这是来自 ViewController 的代码:

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

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section
{

    return [self.fotoArray count];

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"foto_cell";

    FotoCell *cell = [self.collectionView
                        dequeueReusableCellWithReuseIdentifier:cellIdentifier
                        forIndexPath:indexPath];

    Foto *currFoto = [self.fotoArray objectAtIndex:indexPath.row];
    // Set the selectedFotoID 
    [CoreDataManager sharedInstance].selectedFotoId = ((Foto*)[self.fotoArray objectAtIndex:indexPath.row]).objectID;
    NSURL *u = [NSURL URLWithString:currFoto.f_path];
    [self findImage:u in: cell];


    return cell;
}

// Get Photo from Asset Library
-(void)findImage:(NSURL*)u in: (FotoCell*) cell
{

    __block FotoCell *blockCell = cell;
    //
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        if (iref) {
            UIImage *largeimage = [UIImage imageWithCGImage:iref];
            //[largeimage retain];
            UIImage *thumbnail = [UIImage imageWithCGImage:iref scale:0.15 orientation:UIImageOrientationUp];
            [blockCell.fotoThumb setImage:thumbnail];
        }
    };

    //
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"cant get image - %@",[myerror localizedDescription]);
    };



        NSURL *asseturl = u;
        ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetslibrary assetForURL:asseturl
                       resultBlock:resultblock
                      failureBlock:failureblock];

}

这里是我的 Cell.m 类中的代码:

@implementation FotoCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}



- (void)dealloc {
    [_fotoThumb release];
    [super dealloc];
}
@end

最佳答案

我不清楚你的代码。但我以前遇到过这个问题。 原因是您在 cellForItemAtIndexPath 中添加了可见 View (如图像、UILabel 等):

当collectionView序列化时,所有的Cell view在你滚动时重叠在一起。

您可以尝试在您的 Cell 中初始化和 imageView,然后在 cellForItemAtIndexPath 中设置图像名称

关于ios - CollectionView 与单元格重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13452727/

相关文章:

ios - 在 uitableview 单元格内为自定义 View 设置委托(delegate)

iphone - 自定义 setter/getter 中的属性

ios - UIGestureRecognizer 无法识别

iphone - UIButtonTypeRoundedRect 类型的 UIButton 的着色

ios - 尝试在手机上启动 iCloud 应用程序时出现黑屏

ios - iOS 应用程序进行对象检测时图像模糊

Objective-C 为什么我的数组不起作用?

ios - 为什么我的嵌入式 YouTube 播放器不能自动播放?

javascript - 是否可以使用 Expo 将文件写入 iOS 文件系统,以便可以通过 iOS 11 文件应用程序访问该文件

iphone - 在另一个 UIImageView 上注册一个 UIImageView