How do I programmatically create a 3x3 array composed of 9 ImageViews?
How do I programmatically create a 3x3 array composed of 9 ImageViews?
The comment from @carlosfigueira answers your question:
var matriz: [[UIImageView]] = [[UIImageView(), UIImageView(), UIImageView()],
[UIImageView(), UIImageView(), UIImageView()],
[UIImageView(), UIImageView(), UIImageView()]]
matriz[1][1] //<UIImageView: 0x7fca39d34520; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x7fca39d33a70>>
Have you considered using a UICollectionView? The collection view would be responsible for se- lecting the frames of the cells where the images appear, even if the images have different dimensions. Take a look at the code below, which creates an array of 9 images and shows how you would access this array in the collection view protocols.
import UIKit
var arrayDeImagens = [UIImage]()
// só para criar as imagens fake
for _ in 1...9 {
arrayDeImagens.append(UIImage())
}
// MARK: UICollectionViewDataSource
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrayDeImagens.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let imagem = arrayDeImagens[indexPath.item]
/* aqui tu vais instanciar tua subclasse de UICollectionViewCell
que contém uma UIImageView como subview e ira renderizar a tua imagem */
return UICollectionViewCell() // essa linha é só para o compilador ficar quieto
}
// MARK: UICollectionViewDelegateFlowLayout
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return arrayDeImagens[indexPath.item].size
}