点击时更改 UICollectionViewCell 内的视图不起作用
Posted
技术标签:
【中文标题】点击时更改 UICollectionViewCell 内的视图不起作用【英文标题】:Change a view inside of UICollectionViewCell when tapped not working 【发布时间】:2018-05-27 03:51:23 【问题描述】:我在 UICollectionViewCell 中有一个用户头像列表。当用户点击一个时,我想将所选项目添加到集合中并突出显示它以表明它已被点击。
很遗憾,用户界面似乎没有更新。有什么想法吗?
最初我加载图像并将它们设置为圆形。如果需要,我什至可以设置边框颜色,但现在我将其设置为清除。这一切都在加载单元格时起作用:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! UICollectionViewCell
// Configure the cell
let member: UserProfile = groupMembers[indexPath.item]
let imgAvatar = cell.viewWithTag(2) as! UIImageView
imgAvatar.layer.cornerRadius = contactAvatar.frame.size.width / 2
imgAvatar.clipsToBounds = true
imgAvatar.contentMode = UIViewContentMode.scaleAspectFill
imgAvatar.layer.borderWidth = 2.0
imgAvatar.layer.borderColor = UIColor.clear.cgColor
imgAvatar.layer.cornerRadius = 30.0
let downloadURL = NSURL(string: member.avatarUrl)!
imgAvatar.af_setImage(withURL: downloadURL as URL)
return cell
现在这里是当您点击集合中的任何给定 UIImageView 时执行的代码,但它似乎没有更新图像:
///Fired when tapped on an image of a person
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! UICollectionViewCell
let tappedUser: UserProfile = groupMembers[indexPath.item]
let imgAvatar = cell.viewWithTag(2) as! UIImageView
..add item to collection, etc..
//Update imageview to indicate it's been tapped.
DispatchQueue.main.async
contactAvatar.layer.cornerRadius = contactAvatar.frame.size.width / 2
imgAvatar.clipsToBounds = true
imgAvatar.contentMode = UIViewContentMode.scaleAspectFill
imgAvatar.layer.borderWidth = 2.0
imgAvatar.layer.cornerRadius = 30.0
imgAvatar.layer.borderColor = UIcolor.blue.cgColor
运行此代码,它会遇到断点以指示我已点击该项目,但它不会更新 UI。我确信存在一个线程/用户界面问题,其中集合视图没有“重绘”我对图像所做的更改。也许我无法改变集合内视图的外观?
提前感谢您的任何见解。
【问题讨论】:
下面是什么?让 imgAvatar = cell.viewWithTag(2) 为! UIImageView didSelect 中的这一行定义错误 collectionView.dequeueReusableCell.... 【参考方案1】:您的 didSelect 不正确。从集合视图中获取单元格。 cellForItem
///Fired when tapped on an image of a person
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
let cell = collectionView.cellForItem(at:indexPath) as! UICollectionViewCell
let tappedUser: UserProfile = groupMembers[indexPath.item]
let imgAvatar = cell.viewWithTag(2) as! UIImageView
..add item to collection, etc..
//Update imageview to indicate it's been tapped.
DispatchQueue.main.async
contactAvatar.layer.cornerRadius = contactAvatar.frame.size.width / 2
contactAvatar.clipsToBounds = true
contactAvatar.contentMode = UIViewContentMode.scaleAspectFill
contactAvatar.layer.borderWidth = 2.0
contactAvatar.layer.cornerRadius = 30.0
contactAvatar.layer.borderColor = UIcolor.blue.cgColor
使用手机,如果它不起作用,请告诉我。
【讨论】:
【参考方案2】:您为获取单元格实例而编写的代码不正确,请使用下面的代码行,其余的似乎都是正确的,希望通过更改此行将起作用。
let cell = collectionView.cellForItem(at:indexPath) as! UICollectionViewCell
【讨论】:
以上是关于点击时更改 UICollectionViewCell 内的视图不起作用的主要内容,如果未能解决你的问题,请参考以下文章
ios开发之-- tableview/collectionview获取当前点击的cell