UICollectionViewControllerのCellにUIImageViewが表示されない。
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let imageView = UIImageView()
let img = UIImage(named: "image")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imageView.image = img
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.contentView.addSubview(imageView)
return cell
}
}
上のようにしても、コレクションビューに画像が表示されないのはなぜでしょうか。