我有一个带有原型单元的 UICollectionView。单元格加载和图像并显示标签。由于单元格具有不同的大小,它们通过 CollectionViewFlowLayout 进行更改。这很好用。
当我在模拟器中滚动视图时,标签似乎被重复使用并错误地添加到图像上。如何确保不会发生这种情况并且图像在 collectionview 上只有一个标签?
UICollectionView
#pragma mark - Collection view
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
self.Data = [NSArray arrayWithObjects:@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, nil];
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.magazineLayout.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MagazineCell *mCell = (MagazineCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
int item = [indexPath row];
mCell.backgroundColor = [UIColor lightGrayColor];
// Set Image
UIImage *image;
image = [UIImage imageNamed:@"testimage.png"];
mCell.imageView.image = image;
// Set Label
NSString *title = [[NSString alloc] initWithFormat:@"Image %@", self.Data[item]];
[mCell addSubview:[self cellTitle:title indexPath:indexPath]];
return mCell;
}
// Title will be reused and placed wrongly on pictures !
-(UILabel *)cellTitle:(NSString *)name indexPath:(NSIndexPath *)indexPath {
CGSize itemSize = [self collectionView:self.collectionView layout:self.collectionView.collectionViewLayout sizeForItemAtIndexPath:indexPath];
int top = itemSize.height - 40;
int width = itemSize.width;
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, top, width, 40)];
title.textColor = [UIColor blackColor];
title.text = name;
title.backgroundColor = [UIColor whiteColor];
title.alpha = 0.5f;
return title;
}
编辑:解决方法
viewWithTag
工作正常,但我无法重新定位标签。可悲的是,我认为这将是最好的方法。这是我的解决方法,没有viewWithTag
:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MagazineCell *mCell = (MagazineCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
mCell.backgroundColor = [UIColor lightGrayColor];
// Set Image
UIImage *image;
image = [UIImage imageNamed:@"testimage.png"];
mCell.imageView.image = image;
[self cellTitleAndBackground:mCell indexPath:indexPath];
return mCell;
}
-(void)cellTitleAndBackground:(MagazineCell *)mCell indexPath:(NSIndexPath *)indexPath {
// Get title
NSString *name = [[NSString alloc] initWithFormat:@"Image %@", self.Data[indexPath.row]];
// Get current cell size
CGSize itemSize = [self collectionView:self.collectionView layout:self.collectionView.collectionViewLayout sizeForItemAtIndexPath:indexPath];
int top = itemSize.height - 40;
int width = itemSize.width;
// Create title background
UILabel *titleBackground = [[UILabel alloc] initWithFrame:CGRectMake(0, top, width, 40)];
titleBackground.backgroundColor = [UIColor blackColor];
titleBackground.alpha = 0.2f;
titleBackground.tag = 70;
[self removeReusedLabel:mCell tag:70];
[mCell addSubview:titleBackground];
// Create titleLabel
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, top-8, width, 40)];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont boldSystemFontOfSize:14];
titleLabel.text = name;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.tag = 72;
[self removeReusedLabel:mCell tag:72];
[mCell addSubview:titleLabel];
}
-(void)removeReusedLabel:(MagazineCell *)mCell tag:(int)tag {
UILabel *foundLabelBackground = (UILabel *)[mCell viewWithTag:tag];
if (foundLabelBackground) [foundLabelBackground removeFromSuperview];
}
干杯——杰里克