[英]How to fix UICollectionViewCell with UIImageView Overlap Programmatically
I'm create UICollectionView programmatically to uiview (i use single view). 我将以编程方式创建UICollectionView到uiview(我使用单个视图)。 like this
像这样
UICollectionViewFlowLayout *CATLayout=[[UICollectionViewFlowLayout alloc]init];
CATLayout.minimumInteritemSpacing = 2.0f;
CATLayout.minimumLineSpacing = 2.0f;
CATLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
CATLayout.sectionInset = UIEdgeInsetsMake(1.0f, 1.0f, 1.0f, 1.0f);
self.ColStickersListView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, StickersListView.frame.size.width, StickersListView.frame.size.height) collectionViewLayout:CATLayout];
self.ColStickersListView.delegate=self;
self.ColStickersListView.dataSource=self;
self.ColStickersListView.tag=2;
[self.ColStickersListView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollStickersList"];
[self.ColStickersListView setBackgroundColor:[UIColor clearColor]];
[StickersListView addSubview:ColStickersListView];
and 和
UICollectionViewCell *cell = [ColStickersListView dequeueReusableCellWithReuseIdentifier:@"CollStickersList" forIndexPath:indexPath];
// for background selected
NSString *imageName=anObject;
NSString *filename=[NSString stringWithFormat:@"%@/Stickers/List/%@",[appDel DocsPath],imageName];
NSLog(@"%@",filename);
cell.backgroundColor=[UIColor clearColor];
UIImage *image=[UIImage imageNamed:filename];
UIImageView *photoView=[[UIImageView alloc]initWithFrame:CGRectMake(StickersListPadding,StickersListPadding,StickersListThumbSize-(StickersListPadding*2),StickersListThumbSize-(StickersListPadding*2))];
photoView.image=image;
photoView.contentMode=UIViewContentModeScaleAspectFit;
photoView.userInteractionEnabled = YES;
[cell.contentView addSubview:photoView];
return cell;
it's work perfect for display image to cell. 非常适合将图像显示到单元格。
Problem !! 问题!
if scroll page to bottom and scroll return to top again that image in cell it's overlap. 如果将页面滚动到底部,然后再次滚动回到顶部,则该单元格中的图像是重叠的。
How to fix it.!!! 如何解决。 (Programmatically only with single view)
(仅以编程方式使用单一视图)
I suspect because you aren't setting the frame on the UIImageView it is deriving its size from the image that is being set on it. 我怀疑是因为您没有在UIImageView上设置框架,而是从其上设置的图像派生其大小。 So in some cases it might overflow its parent view -> the cell in this case.
因此,在某些情况下,它可能会溢出其父视图->单元格。
I suggest you setup some constraints on the UIImageView, this can be done programmatically. 我建议您在UIImageView上设置一些约束,这可以通过编程来完成。 So try to set left, right, top and bottom constraints on the UIIMageView so that it stays within the bounds of the cell
因此,尝试在UIIMageView上设置左,右,上和下约束,以使其停留在单元格的范围内
During scroll 滚动期间
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
is beeing called repeatively.. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
被重复调用。
the means your are adding UIImageView *photoView
repeatedly to cell.contentView
这意味着您将
UIImageView *photoView
重复添加到cell.contentView
UIImageView *photoView=[[UIImageView alloc]initWithFrame:CGRectMake(StickersListPadding,StickersListPadding,StickersListThumbSize-(StickersListPadding*2),StickersListThumbSize-(StickersListPadding*2))];
//codes..
[cell.contentView addSubview:photoView];
doing something like this prevent that: 做这样的事情可以防止:
UICollectionViewCell *cell = [ColStickersListView dequeueReusableCellWithReuseIdentifier:@"CollStickersList" forIndexPath:indexPath];
if (cell == nil)
{
UIImageView *photoView=[[UIImageView alloc]initWithFrame:CGRectMake(StickersListPadding,StickersListPadding,StickersListThumbSize-(StickersListPadding*2),StickersListThumbSize-(StickersListPadding*2))];
// codes...
[cell.contentView addSubview:photoView];
}
Another way of solving this is by creating a custom collection cell: 解决此问题的另一种方法是创建一个自定义收集单元:
// CustomCollectionCell.h
@interface YourCollectionCell : UICollectionViewCell
@property (nonatomic) UIImageView *photoView;
@end
// CustomCollectionCell.m
@implementation YourCollectionCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// and setting up you imageview here
self.photoView=[[UIImageView alloc]initWithFrame:YourRect];
self.photoView.contentMode=UIViewContentModeScaleAspectFit;
self.photoView.userInteractionEnabled = YES;
[self.contentView addSubview:self.photoView];
}
return self;
}
and using it like: 并像这样使用它:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCollectionCell *cell = (CustomCollectionCell *)[ColStickersListView dequeueReusableCellWithReuseIdentifier:@"CollStickersList" forIndexPath:indexPath];
cell.photoView.image=image;
return cell;
}
Hope i've helped you, happy coding.. Cheers & Good night! 希望我对您有所帮助,祝您编程愉快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.