我打算在單個tableView單元中採用多個collectionView。單個TableCell中的多個collectionViews
我真正想要的是以這種方式顯示專輯照片(UI很重要)。見圖片:
如何使該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>
- 請提出任何其他好辦法來實現這個UI?
- 如果我製作此用戶界面的方式正確,可能會導致崩潰的原因是什麼?
嘗試集合視圖自定義佈局https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest – suhit