我希望動態地將UIImageView數組添加到視圖中。當我通過一個for循環迭代似乎只有最後的UIImageView添加,從陣列而不是所有:從數組動態添加UIImageView's
// same image to be used
let imageName = "img.png"
let image = UIImage(named: imageName)
// array of UIImageViews
var images : [UIImageView?] = Array(count: 10, repeatedValue: UIImageView(image: image))
// iterate and add to view
for i in 0...9 {
// setup UIImageView
// this is how I know only the last UIImageView is added to the parent view
// the y cordinate is about 10 points down (this is expected of i+i array size with of 10)
images[i]?.frame = CGRect(x: 10, y: CGFloat(i+i), width: image!.size.width, height: image!.size.height)
// add to parent view
parentView.addSubview(images[i]!)
}
有沒有一個簡單的解決這個?我覺得我好像缺少一些公然的東西。
你設置的parentView斷點,看着它的子視圖數組? – jarryd
@ Helium3我可以在調試控制檯中找到該數組,但是我註銷了父級中的所有子視圖,並且當我在原始for循環內註銷子視圖時,也僅記錄了1個UIImageView – rambossa
,每個不同的UIImageView都會得到記錄,他們只是似乎替換 – rambossa