在UICollectionViewCell中添加UICollectionView

我正在使用Swift为我工作的医院构build一个iOS应用程序。

不知何故,在一个特定的function,我必须把一个UICollectionView内的UICollectionViewCell。 我想要实现的是父视图的每个内容(垂直滚动)将有几个孩子(这可以滚动水平)依赖于父行。

为了说明,我必须显示医生(姓名和照片)的名单,然后我必须在他们的姓名和照片下方显示他们的每个练习时间表。 练习时间表将取决于每个医生。 所以,我必须把它放在收集视图。

我尝试了几个我在网上find的解决scheme,但到现在为止我还是无法接近它。

我无法解决的最大问题是:我不知道代码中加载子数据源的位置(医生时间表),以及何时加载它,因为我不能在下面有两个func

collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

这是我想要达到的目标

收集视图的设计

ui图像和医生姓名(uilabel)在父集合视图单元格中(垂直滚动),然后框中的所有内容(练习日n练习时间)都是子集合视图(水平滚动)

PS:有很多医生,每个医生都有几个练习日。

请帮助我如何做到这一点

如果你真的想在一个collectionViewCell中插入一个collectionView,那么就有一个非常简单的步骤。 创build一个UICollectionView的实例,并将其添加到collectionViewCell中。 你可以使用这个例子,如果你喜欢。

 // // ViewController.swift // StackOverFlowAnswer // // Created by BIKRAM BHANDARI on 18/6/17. // Copyright © 2017 BIKRAM BHANDARI. All rights reserved. // import UIKit class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { let cellId = "CellId"; //Unique cell id override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .red; //just to test collectionView.register(Cell.self, forCellWithReuseIdentifier: cellId) //register collection view cell class setupViews(); //setup all views } func setupViews() { view.addSubview(collectionView); // add collection view to view controller collectionView.delegate = self; // set delegate collectionView.dataSource = self; //set data source collectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true; //set the location of collection view collectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true; // top anchor of collection view collectionView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true; // height collectionView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true; // width } let collectionView: UICollectionView = { // collection view to be added to view controller let cv = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout()); //zero size with flow layout cv.translatesAutoresizingMaskIntoConstraints = false; //set it to false so that we can suppy constraints cv.backgroundColor = .yellow; // test return cv; }(); //deque cell func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath); // cell.backgroundColor = .blue; return cell; } // number of rows func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 5; } //size of each CollecionViewCell func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.width, height: 200); } } // first UICollectionViewCell class Cell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { let cellId = "CellId"; // same as above unique id override init(frame: CGRect) { super.init(frame: frame); setupViews(); collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId); //register custom UICollectionViewCell class. // Here I am not using any custom class } func setupViews(){ addSubview(collectionView); collectionView.delegate = self; collectionView.dataSource = self; collectionView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true; collectionView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true; collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true; collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true; } let collectionView: UICollectionView = { let layout = UICollectionViewFlowLayout(); layout.scrollDirection = .horizontal; //set scroll direction to horizontal let cv = UICollectionView(frame: .zero, collectionViewLayout: layout); cv.backgroundColor = .blue; //testing cv.translatesAutoresizingMaskIntoConstraints = false; return cv; }(); func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath); cell.backgroundColor = .red; return cell; } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 5; } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: self.frame.width, height: self.frame.height - 10); } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } 

示例截图

有多种方法可以解决垂直列表集合中另一个水平集合的问题。

最简单的方法就是使ViewController将主UICollectionView呈现给dataSouce和delegate,以用于这两个集合视图。 您也可以在单元格内设置收集视图,也可以从这里获取。

这篇关于在表视图中放置集合视图的文章以非常复杂的方式解释了这个问题, 同样的代码也可以在这里find 。

在collection视图单元格中添加collectionView,并在collectionviewclass.swift中添加delagate方法。 然后在collectionView的cellForrowatindexpath中传递要在单元格中显示的列表。 如果你没有成功实施它,那么让我知道。 我会为你提供代码,因为我已经以这种方式实现了它。