【发布时间】:2016-08-13 13:48:15
【问题描述】:
再次。
我刚刚使用以下代码完成了字典的单例示例
在文件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
所以singleton 与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}
怎么可能把这张图片设置为背景??!?!
非常感谢!
【问题讨论】:
-
您的按钮是否以编程方式创建?
标签: ios swift swift2 ios9 swift-dictionary