2016-06-15 19 views
0

この拡張機能は、CollectionView内にUIImageViewを1つだけ提供します。より多くの画像を表示するには、写真の配列のサイズに依存します。 Photos配列にはUrlからimages(String)までの配列が含まれます。UICollectionView Swiftで複数の画像を表示する方法

extension FlatViewController: UICollectionViewDataSource{ 
     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
      return (flat.photos?.count)! 
     } 
     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCollectionViewCell 

      cell.imageView.loadImageFromURL(NSURL(string: flat.image!)!) 
      return cell 
     } 
    } 

答えて

0

さてあなたは、単純にInterface Builderに行くと、あなたが望むようごCollectionViewCellImageView elementsの量に置くことができます。次に、CollectionViewControllerに、追加したImageView要素を参照するIBOutletsを作成します。次に、機能cellForItemAtIndexPathの中で、それぞれIBOutlet ImageView'sの各画像URLを差し込みます。

これでうまくいくはずです。

0

UICollectionViewCellサブクラスでは、フォトアレイのサブ配列に基づいてUIImageViewをプログラムで作成します。これを行うには、2つのプロトコルを設定し、FlatViewControllerがサブクラスの代理人である場合にデリゲートパターンを実装します。

プロトコルは次のようになります:

protocol PhotoDelegate { 

    var photos: [[NSURL]] { get set } 

} 

protocol HasPhotos { 

    var delegate: PhotoDelegate! { get set } 

} 

デリゲートパターンを使用すると、サブクラスからphotosアレイにアクセスできるようになります。デキューされたセル変数のデリゲートプロパティを設定し、サブクラスとしてキャストする必要があります(cellForRowAtIndexPath)。ご質問があればそう

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "<Insert Identifier>", for: indexPath) as! Subclass 
cell.delegate = self 

のようにちょうど頼みます!私は助けが大好きです。

関連する問題