我使用以下代码将view
添加到scrollview
并将view
保存到viewAll
var viewAll = [Int: UIView]()
i = 0
for pack in packs as! [[String: AnyObject]] {
let packId = Int(pack["packId"] as! String)
let packTitle = pack["packTitle"] as! String
let view = CategoryClass.createMyClassView()
view.pcId.text = packId
view.pcTitle.text = packTitle
viewAll[i] = view
i = i+1
myScrollView.addSubview(view)
}
工作正常。
现在,我想再次访问视图的子项:
for view in viewAll {
view.value.pcId.text = "TEST"
}
此行不起作用:view.value.pcId.text = "TEST"
CategoryClass:
class CategoryClass: UIView {
@IBOutlet weak var pcId: UILabel!
@IBOutlet weak var pcTitle: UILabel!
class func createMyClassView() -> CategoryClass {
let myClassNib = UINib(nibName: "Category", bundle: nil)
return myClassNib.instantiate(withOwner: nil, options: nil)[0] as! CategoryClass
}
}
答案 0 :(得分:1)
这是因为投射,您需要更改
false
到
var viewAll = [Int: UIView]()
//
也更改此
var viewAll = [Int: CategoryClass]()
到
view.value.pcId.text = "TEST"