class MDCollectionViewLayoutAttributes : UICollectionViewLayoutAttributes
{
var color: UIColor = UIColor.whiteColor()
var image : UIImageView!
override func copyWithZone(zone: NSZone) -> AnyObject
{
let newAttributes: MDCollectionViewLayoutAttributes = super.copyWithZone(zone) as! MDCollectionViewLayoutAttributes
newAttributes.color = self.color.copyWithZone(zone) as! UIColor
newAttributes.image = UIImageView(frame:newAttributes.bounds)
newAttributes.image.image = UIImage(named:"Appetizer.png")
newAttributes.image = self.image.copyWithZone(zone) as! UIImageView// this is giving me an error
return newAttributes
}
}
我需要在下面的代码中声明一个图像以将其设置为剖面背景,但它给了我一个错误。
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]?
{
let attributes = super.layoutAttributesForElementsInRect(rect)
var allAttributes = [UICollectionViewLayoutAttributes]()
if let attributes = attributes
{
for attr in attributes
{
if (attr.representedElementCategory == UICollectionElementCategory.Cell && attr.frame.origin.x == self.sectionInset.left)
{
let decorationAttributes = SBCollectionViewLayoutAttributes(forDecorationViewOfKind: "sectionBackground", withIndexPath: attr.indexPath)
// decorationAttributes.color = UIColor.brownColor().colorWithAlphaComponent(1)
任何帮助?
答案 0 :(得分:0)
UIImageView不符合NSCopying协议,这就是copyWithZone无法正常工作的原因。您可以为实现NSCopying一致性的UIImageView或其子类创建扩展。