【问题标题】:error: Static member copyWithZone cannot be used on instance of type UIImageView错误:静态成员 copyWithZone 不能用于 UIImageView 类型的实例
【发布时间】:2017-01-12 12:18:29
【问题描述】:
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)

有什么帮助吗?

【问题讨论】:

    标签: ios swift uiimageview uicollectionview


    【解决方案1】:

    UIImageView 不符合 NSCopying 协议,这就是 copyWithZone 不起作用的原因。您可以创建 UIImageView 或其子类的扩展,以实现 NSCopying 一致性。

    【讨论】:

    • 有什么例子吗?或任何示例链接?
    猜你喜欢
    • 2018-05-08
    • 2016-06-24
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    相关资源
    最近更新 更多