CollectionView无法更改UIImageView的框架

时间:2014-08-13 09:48:22

标签: ios objective-c uiimageview uicollectionview

我在UICollectionView上创建了包含imageView的内容。我想调整此imageView的大小以使框架等于单元格尺寸。我尝试过使用:

self.homeImage.frame =CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

在我的CollectionViewCell子类中,但没有做任何改变大小的事情。如何使用代码动态更改imageView框架?

我已经仔细检查了数据源和委托是否已连接。插座也正确连接到imageView。

collectionViewCell子类

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}


 - (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {

         self.homeImage.frame =CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

    }
    return self;
 }

cellForItemAtIndexPath

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *identifier = @"Cell";

    FRGWaterfallCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];



    UIImage *theImage = [[homesDic objectAtIndex:indexPath.item] objectForKey:@"image"];

    cell.homeImage.image = theImage;

    cell.backgroundColor = [UIColor redColor];


    return cell;
}

2 个答案:

答案 0 :(得分:1)

取消选中Autolayout文件中的FRGWaterfallCollectionViewCell.xib

答案 1 :(得分:0)

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *identifier = @"cell";    
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];    
if (cell == nil)    
{    

        cell = [[UICollectionViewCell alloc] init];    
}    
for (UIView *v in [cell subviews])       
{    

    if ([v isKindOfClass:[UIView class]])  { 

        [v removeFromSuperview];    
    }    

}    
UIImageView *imgMiddleView;    
imgMiddleView = [[UIImageView alloc] initWithFrame:cell.frame];    
imgMiddleView.contentMode = UIViewContentModeScaleToFill;    
imgMiddleView.clipsToBounds = YES;    
[imgMiddleView setUserInteractionEnabled:YES];    
imgMiddleView.tag = indexPath.row;    

[cell addSubview:imgMiddleView];    

return cell;   

}