我想在用户触摸单元格时更改边框颜色。我的UICollectionView中包含以下代码,但无法正常工作:
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.user["avatar"] = self.avatars[indexPath.row]
do {
try self.user.save()
} catch {
print(error)
}
//self.performSegueWithIdentifier("returnToProfile", sender: user)
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! AvatarViewCell
cell.layer.borderWidth = 5.0
cell.layer.borderColor = UIColor.yellowColor().CGColor
}
谢谢!
编辑:我在被标记为重复的问题中尝试了投票最多的答案,但仍然无法正常工作。cell.layer.borderColor = UIColor.yellowColor().CGColor
不会改变颜色。
也许这与我如何定义单元格有关?
我在触摸时使用数据源更改单元格背景:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! DateCell
if !days.contains(Functions.tools.nextThreeWeeks().long[indexPath.row]) {
days.append(cell.fullDate)
cell.backgroundColor = UIColor(red:0.96, green:0.72, blue:0.35, alpha:1)
} else {
days = days.arrayRemovingObject(cell.fullDate)
cell.backgroundColor = UIColor.whiteColor()
}
print(days)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句