【发布时间】: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....
标签: ios swift uicollectionview uicollectionviewcell grand-central-dispatch