2017-08-31 16 views
0

我打算在單個tableView單元中採用多個collectionView。單個TableCell中的多個collectionViews

我真正想要的是以這種方式顯示專輯照片(UI很重要)。見圖片:

enter image description here

如何使該UI?直到現在,我已經採取了TableView - > TableCell(Single) - > 3個不同的collectionView在單元格內。

代碼:

CustomGalleryController

class CustomGalleryController: UIViewController { 

    var arrayOfPHAsset = [PHAsset]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     CustomAlbum().fetchCustomAlbumPhotos { (array) in 
      self.arrayOfPHAsset = array 
      CustomGalleryTableCell().firstCollectionArray = array 
      CustomGalleryTableCell().secondCollectionArray = array 
      CustomGalleryTableCell().thirdCollectionArray = array 
      //self.collectionView.reloadData() 
     } 



    } 

extension CustomGalleryController : UITableViewDelegate{ 

} 

extension CustomGalleryController : UITableViewDataSource { 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CustomGalleryTableCell", for: indexPath) as! CustomGalleryTableCell 

     return cell 
    } 
} 

CustomGalleryTableCell

class CustomGalleryTableCell: UITableViewCell { 


    @IBOutlet var firstCollectionView: UICollectionView! 
    @IBOutlet var secondCollectionView: UICollectionView! 
    @IBOutlet var thirdCollectionView: UICollectionView! 

    var firstCollectionArray = [PHAsset]() 
    var secondCollectionArray = [PHAsset]() 
    var thirdCollectionArray = [PHAsset]() 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 

extension CustomGalleryTableCell : UICollectionViewDataSource{ 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
//  if collectionView == firstCollectionView { 
//   return 1;//firstCollectionArray.count 
//  }else if collectionView == secondCollectionView { 
//   return 1;//secondCollectionArray.count 
//  }else{ 
//   return 1;//thirdCollectionArray.count 
//  } 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     if collectionView == firstCollectionView { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FirstCollectionCell", for: indexPath) as! FirstCollectionCell 

      return cell 
     }else if collectionView == secondCollectionView { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SecondCollectionCell", for: indexPath) as! SecondCollectionCell 

      return cell 
     }else{ 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ThirdCollectionCell", for: indexPath) as! ThirdCollectionCell 

      return cell 
     } 
    } 
} 

但要崩潰:

終止應用程序由於未捕獲的異常NSInvalidArgumentException '的, 原因:' - [UITableView中的CollectionView:numberOfItemsInSection:]: 無法識別的選擇發送到實例0x1049ed600

po 0x1049ed600給出:

<UITableView: 0x1049ed600; frame = (0 0; 414 736); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x17424b550>; 

層=; contentOffset:{0,0}; contentSize: {414} 44>

  1. 請提出任何其他好辦法來實現這個UI?
  2. 如果我製作此用戶界面的方式正確,可能會導致崩潰的原因是什麼?
+0

嘗試集合視圖自定義佈局https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest – suhit

回答

1

這將實現利用chiahsien作出CHTCollectionViewWaterfallLayout。我已經使用過這個庫,我必須說它非常棒。

可以使用吊艙安裝:

pod 'CHTCollectionViewWaterfallLayout/Swift' 

在斯威夫特的演示可以發現here

唯一不同於此示例的是,在頂部的每列上都有一定的填充,但我相信這也是可以實現的。

+0

它取決於圖像大小?在這個圖書館? –

+0

是的,但如果圖像大小始終相同,則它可以毫無問題地工作。 –

+0

是的,這就像這3列的問題是相同的,如果你垂直滾動。它不是隨機的 –

1

請提出任何其他和良好的方式來實現這個用戶界面?

如果你的目標是要達到什麼樣的你在截圖提,就沒有必要來實現它作爲包含collectionviews,取而代之的是,直接的方式與UICollectionViewLayout工作的tableview。

有一個小竅門,爲節省一些時間和精力的目的,你可以找到能爲你做的工作,如圖書館:

或者,如果你有興趣在重新排序的項目:

+0

但是如何實現CHTCollectionViewWaterfallLayout中特定列的頂部填充? –

+0

如果我說得對,有兩個屬性,你要求什麼:'minimumColumnSpacing'和'minimumInteritemSpacing' –

+0

不,像頂部填充你可以看到每列是不同的。 –

0

初學者與swift和Xcode在這裏。我一直在尋找解決方案來在單個TableViewCell中填充兩個CollectionView。儘管這可能不適合您的情況,但您的問題的標題匹配:-)最後設法通過使用故事板中的標籤屬性來完成。

我是這樣做的:

  • 先給在故事板
  • 在你ViewControllerDataSource標籤編號到每個的CollectionView,檢查它是否是由if語句使用權的CollectionView和返回任何真實需要(numberOfItemsInSection,cellForItemAt)。

    extension ViewController: UICollectionViewDataSource { 
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
         if collectionView.tag == 0 { 
          return imageArray1.count 
         } else { 
          return imageArray2.count 
         } 
        } 
    } 
    

希望這有助於!

相關問題