Iphone IOS:在UICollectionView单元格上添加覆盖图像
我正在使用Iphone IOS:在UICollectionView单元格上添加覆盖图像,iphone,ios,uicollectionview,Iphone,Ios,Uicollectionview,我正在使用UICollectionView生成图像库。我在UICollectionView单元格中使用UIImage加载图像。我需要长按选择UICollectionView单元格。只需拿起didSelectItemAtIndexPath代理回调,抓取单元格,并将图像添加为子视图 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionView
生成图像库。我在UICollectionView
单元格中使用UIImage
加载图像。我需要长按选择UICollectionView
单元格
。只需拿起didSelectItemAtIndexPath
代理回调,抓取单元格,并将图像添加为子视图
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
YourCollectionViewCell *cell = (YourCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
UIImageView *yourImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];//whichever frame you want it to appear at
yourImageView.image = [UIImage imageNamed:@"yourImageName"];//set the image
[cell.yourBaseImage addSubview:yourImageView];//or add it to whatever part of the cell you want
}
或者,也可以在故事板内部设置一个隐藏的imageView。然后将其取消隐藏并将图像设置在didSelectItemAtIndexPath
中,效果非常好。谢谢,没问题!祝你一切顺利!,如何使用DIDDeselectItemIndexPath方法删除特定单元格的覆盖图像?谢谢你难道不认为这会把滚动和单元格重用搞砸吗?当手机离开屏幕后又重新打开时会发生什么?它失去了是否被选中的状态。正确的方法是改变模型对象的状态,让单元格从中提取。最后一行的“yourBaseImage”是什么?