集合视图与单元格重叠

时间:2012-11-19 11:21:02

标签: ios objective-c cocoa-touch uikit uicollectionview

我的目标是在我的视图的页脚上有一个集合视图。细胞充满了照片库中的照片。我不知道为什么但细胞相互重叠。有人知道为什么吗?以下是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

2 个答案:

答案 0 :(得分:1)

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

当collectionView序列时,滚动时所有Cell视图都重叠在一起。

你可以尝试在你的Cell中使用init和imageView,然后在cellForItemAtIndexPath中设置图像名称

答案 1 :(得分:0)

您必须设置itemSize实例的UICollectionViewFlowLayout