2

再次。

我刚刚完成了一个字典的单例示例,文件中包含以下代码Recipes.swift

 static let sharedInstance = Recipes()

var imagesOfChef = [Int : chefImages]()
 struct chefImages {
        var objidChef: String!
        var imageChef: UIImage!
    }

当一个过程完成时,我有这个代码

let singleton = Recipes.sharedInstance
singleton.imagesOfChef = self.imagesOfChef

所以 the 和singletonhas 有相同的字典imagesOfChef

在下一个视图中,我想访问 Dictionary 的所有数据,并使用 Dictionary 具有的图像之一设置按钮的背景图像。所以在cellForItemAtIndexPath我有以下代码

let test = userPointer.objectId
    let otherReferenceToTheSameSingleton = Recipes.sharedInstance




for i in 0...otherReferenceToTheSameSingleton.imagesOfChef.count - 1  {
    print(otherReferenceToTheSameSingleton.imagesOfChef[i]!.objidChef) //it prints the object id's of the Chef normally

    if otherReferenceToTheSameSingleton.imagesOfChef[i]!.objidChef  == test! {
        print("done")
        print(otherReferenceToTheSameSingleton.imagesOfChef[i]!.imageChef)
        cell.avatarOutlet.setImage(otherReferenceToTheSameSingleton.imagesOfChef[i]!.imageChef, forState: .Normal) //but it doesnt set the Image as a background

    }

}

因此,当 Chef 的对象 id 与 Dictionary 内的对象 id 匹配时,我想将同一行中的图像设置为背景。

但事实并非如此!

虽然当if statement你看到正确的时候我尝试打印图像并且我得到了

<UIImage: 0x1582c9b50>, {60, 60}

怎么可能将此图像设置为背景??!?!

非常感谢!

4

1 回答 1

1

如果以编程方式创建,则设置自定义按钮类型

 let button   = UIButton(type: UIButtonType.Custom) as UIButton

如果它来自 Stroyboard,请更改那里的类型。

于 2016-08-16T05:55:10.177 回答