我有一个UICollectionView
和一个自定义UICollectionViewCell
。
UICollectionViewCell
包含自定义UIView
。
我需要根据数组中的属性更改UIView的颜色。
我在故事板中创建了自定义UIView
,为其分配了适当的类,甚至是标记。
我正在尝试在cellForItemAtIndexPath中更改自定义circleView的颜色
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("circleCell", forIndexPath: indexPath) as! BRGameCollectionViewCell
var circle = BRGameManager.sharedInstance.gameArray[indexPath.section][indexPath.row] as BRCircle!
cell.circleView.circleColor = UIColor.redColor()
return cell
}
即使通过circleView
获取tag
,我也尝试了它,但它也不起作用。
我做错了什么?
我的自定义视图类看起来像这样:
class BRCircleView: UIView {
var circleSize : CGFloat = 10
var circleColor : UIColor!
override init(frame: CGRect) {
if( frame == CGRectZero){
var newFrame = CGRectMake(0, 0, self.circleSize, self.circleSize)
}
self.circleColor = UIColor(rgba: GlobalConstants.colors.hexColorGreen)
super.init(frame: frame)
}
convenience init(frame: CGRect, circleColor : UIColor){
self.init(frame: frame)
self.circleColor = circleColor
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(rect: CGRect) {
self.layer.cornerRadius = self.frame.size.width/2
self.clipsToBounds = true
self.backgroundColor = self.circleColor
}
}
答案 0 :(得分:0)
如果您想反映circleView
的背景颜色变化,则需要重新绘制视图的内容。使用setNeedsDisplay
UIView
方法可以做到这一点。{{3} }
所以在返回单元格之前,添加这行代码。
...
cell.circleView.circleColor = UIColor.redColor()
cell.circleView.setNeedsDisplay() // add this line.
return cell