自定义UICollectionViewCell中的小UIImage视图(Small UIImage view in custom UICollectionViewCell)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
自定义UICollectionViewCell中的小UIImage视图(Small UIImage view in custom UICollectionViewCell)

我有UICollectionViewCell

#import "DBPhotoCollectionViewCell.h" #define IMAGEVIEW_BORDER_LENGTH 5 @implementation DBPhotoCollectionViewCell - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code [self setup]; } return self; } /* Need to implement the initWithCoder method since this class will be created from the storyboard */ -(id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self){ [self setup]; } return self; } /* Create the UIImageView and add it to the cell's contentView in code. */ -(void)setup { self.imageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, IMAGEVIEW_BORDER_LENGTH, IMAGEVIEW_BORDER_LENGTH)]; [self.contentView addSubview:self.imageView]; } @end

并从CollectionViewController类调用此Cell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Photo Cell"; DBPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; cell.imageView.image = [UIImage imageNamed:@"Astronaut.jpg"]; // Configure the cell return cell; }

但是当我开始我的应用程序时,我有一个问题,我的图片显示的是小尺寸。 我错过了什么?

http://screencast.com/t/ia8tE05P6yc http://screencast.com/t/33N4AhT7

I have UICollectionViewCell

#import "DBPhotoCollectionViewCell.h" #define IMAGEVIEW_BORDER_LENGTH 5 @implementation DBPhotoCollectionViewCell - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code [self setup]; } return self; } /* Need to implement the initWithCoder method since this class will be created from the storyboard */ -(id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self){ [self setup]; } return self; } /* Create the UIImageView and add it to the cell's contentView in code. */ -(void)setup { self.imageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, IMAGEVIEW_BORDER_LENGTH, IMAGEVIEW_BORDER_LENGTH)]; [self.contentView addSubview:self.imageView]; } @end

And call this Cell from CollectionViewController class

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Photo Cell"; DBPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; cell.imageView.image = [UIImage imageNamed:@"Astronaut.jpg"]; // Configure the cell return cell; }

But I have one problem when I start my application my picture is showed with small size. What I missed?

http://screencast.com/t/ia8tE05P6yc http://screencast.com/t/33N4AhT7

最满意答案

cell.imageView,imageView对象与您在Interface Builder的单元格上添加的对象不同。 UICollectionViewCell具有正在访问的默认大小UIImageView,您可以通过在接口构建器中链接subClass DBPhotoCollectionViewCell的imageView来访问您在customView的cellForIndexPath方法中的customView imageView,您不需要添加[self.contentView addSubview:self.imageView]; 在设置方法中。

当您添加到单元格contentView时,第二种方法是为imageView分配标记值

/* Create the UIImageView and add it to the cell's contentView in code. */ -(void)setup { self.imageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, IMAGEVIEW_BORDER_LENGTH, IMAGEVIEW_BORDER_LENGTH)]; self.imageView.tag=1; [self.contentView addSubview:self.imageView]; } //And access imagView in cellForIndexPath - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Photo Cell"; DBPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; UIImageView *imageView=[cell.contentView viewWithTag:1]; imageView.image = [UIImage imageNamed:@"Astronaut.jpg"]; // Configure the cell return cell; }

cell.imageView, imageView object is not same as which you have added on cell of Interface Builder. UICollectionViewCell has default size UIImageView which are accessing, you can access your customView imageView in cellForIndexPath method of collectionView by linking imageView of subClass DBPhotoCollectionViewCell in interface builder you don't need to add [self.contentView addSubview:self.imageView]; in setup method.

Second approach would be assign tag value to imageView when you adding to cell contentView

/* Create the UIImageView and add it to the cell's contentView in code. */ -(void)setup { self.imageView = [[UIImageView alloc] initWithFrame:CGRectInset(self.bounds, IMAGEVIEW_BORDER_LENGTH, IMAGEVIEW_BORDER_LENGTH)]; self.imageView.tag=1; [self.contentView addSubview:self.imageView]; } //And access imagView in cellForIndexPath - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Photo Cell"; DBPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; UIImageView *imageView=[cell.contentView viewWithTag:1]; imageView.image = [UIImage imageNamed:@"Astronaut.jpg"]; // Configure the cell return cell; }

更多推荐

本文发布于:2023-04-13 12:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/641b5d9a9729b10ccd739c6a1cff7dd0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   视图   UICollectionViewCell   custom   UIImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!