2016-06-15 40 views
0

此擴展在CollectionView中只提供一個UIImageView。如何顯示更多圖像,取決於照片數組的大小。照片數組包含Url到圖像的數組(字符串)。如何在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

那麼你可以直接進入界面生成器和擺在ImageView elements量到您的CollectionViewCell正如您希望。然後創建IBOutlets以引用您在CollectionViewController中添加的ImageView元素。然後在功能cellForItemAtIndexPath內插入每個IBOutlet ImageView's的每個圖像URL。

這應該工作。

0

UICollectionViewCell子類以編程方式創建基於照片數組的子陣列的UIImageView。您可以通過設置兩個協議並實現委託模式來實現此目的,其中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 

如果您有任何問題只是問!我喜歡幫助。

相關問題