2017-12-29 4 views
0

Xcode 9.2とSwift 4.0を使用していますCollectionViewControllerで画像をセルに割り当てます。

Xcodeが生成したUICollectionViewControllerがあります。私はそれを少しカスタマイズし、私のコードはこのように見える。

class CollectionViewController: UICollectionViewController { 

var images = [UIImage(named: “image1“), [UIImage(named: “image2“), [UIImage(named: “image3“)] 

//stuff deleted 
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
    code 
    } 
} 

私は下のセル上の画像のプロパティがあると予想していたので、これは私が書くことだろうと思ったスウィフトです。

class CollectionViewController: UICollectionViewController { 

var images = [UIImage(named: “image1“), [UIImage(named: “image2“), [UIImage(named: “image3“)] 

//stuff deleted 
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
    cell.image = images[indexPath.row] //this doesn’t compile 
    } 
} 

コードはコンパイルされません。

はいこれを自分で解決しようとしましたが、ここではStackOverflowなどのウェブ上の例は、私がまだコンパイルしたいものと非常によく似ています。

ありがとうございます。

答えて

0

デフォルトではUICollectionViewCellにはimageというプロパティがありません。そのようなプロパティがある場合は、型にキャストするか、または与えられたイメージを持つUIImageViewを作成し、セルに追加する必要があります。

+0

今、私はそれを働かせました。ありがとうございました! – Steve

関連する問題