Ios UICollectionView和选定的UICollectionViewCell
我正在尝试使用一个由上面的Ios UICollectionView和选定的UICollectionViewCell,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在尝试使用一个由上面的UIImageView和UIView组成的自定义单元格来执行一个简单的UICollectionView 未选择单元格时,单元格顶部的ui视图将backgroundColor属性设置为UIColor(红色:1,绿色:1,蓝色:1,alpha:0.5) 我对选择有意见。调用了collectionView:didSelectItemAtIndexPath:方法,但是当我更改前面描述的UIView时,什么也没有发生 这是我的收藏视图的代码: class TestCollecti
UIImageView
和UIView
组成的自定义单元格来执行一个简单的UICollectionView
未选择单元格时,单元格顶部的ui视图将backgroundColor
属性设置为UIColor(红色:1,绿色:1,蓝色:1,alpha:0.5)
我对选择有意见。调用了collectionView:didSelectItemAtIndexPath:
方法,但是当我更改前面描述的UIView
时,什么也没有发生
这是我的收藏视图的代码
:
class TestCollectionViewController: UICollectionViewController
{
var items = [1, 2, 3]
let cellId = "Cell"
override func viewDidLoad()
{
self.collectionView.allowsMultipleSelection = true
self.collectionView.delaysContentTouches = false
}
override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int
{
return items.count
}
override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId,
forIndexPath: indexPath) as TestingCollectionViewCell
let item = items[indexPath.row]
cell.imageView.image = UIImage(named: "img")
cell.overlayView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)
return cell
}
override func collectionView(collectionView: UICollectionView!, didSelectItemAtIndexPath indexPath: NSIndexPath!)
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId,
forIndexPath: indexPath) as TestingCollectionViewCell
cell.overlayView.backgroundColor = UIColor.clearColor()
}
override func collectionView(collectionView: UICollectionView!, didDeselectItemAtIndexPath indexPath: NSIndexPath!)
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId,
forIndexPath: indexPath) as TestingCollectionViewCell
cell.overlayView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)
}
}
如果这个代码段还不够,这里是您不应该在didSelectItemAtIndexPath(或DIDSELECT)中对一个新单元格进行排队-这是在创建(或重用)一个新单元格,而不是获取您选择的单元格。用这个代替
var cell = collectionView.cellForItemAtIndexPath(indexPath)
对我来说,以下代码工作得更好(rdelmar的代码有时崩溃,这可能是Swift中的一个bug):
编辑:我忘了提到:我的collectionView项目基于我所投给的它们自己的类。uiBackground是该类的一个元素Swift 3.0
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
//saving index path of the image loaded from imageproperty_array
selected_imagepath = imageproperty_array[indexPath.row] as String
//saving index of the selected cell item as Int
selected_imageindex = indexPath.row
}
let cell = collectionView.cellForItem(at: indexPath)
非常感谢你,已经为此奋斗了一段时间!但是,我的新映像是在UICLoeDebug视图的中间绘制的,而不是在单击的单元格中绘制的。知道为什么吗?@kakubei你在问什么新形象?您是在询问imageView.image还是overlayView?我为该单元格装箱了一个带有图像的me imageView,在重新加载数据时,图像会移动到另一个单元格。我不明白为什么。@kakubei你应该问一个问题,并展示你正在使用的代码。如果不看你的代码,我就不能给你一个好的答案。
let cell = collectionView.cellForItem(at: indexPath)