我正在使用 Swift为我工作的医院构建一个iOS应用程序. 不知何故,在一个特定的功能中,我必须在UICollectionViewCell中放置一个UICollectionView.我想要实现的是父UICollectionView(垂直滚动)的每个内
不知何故,在一个特定的功能中,我必须在UICollectionViewCell中放置一个UICollectionView.我想要实现的是父UICollectionView(垂直滚动)的每个内容都有几个子节点(可以水平滚动),具体取决于父行.
为了说明,我必须显示医生名单(姓名和照片),然后我必须在他们的名字和照片下方显示他们的每个练习时间表.实践时间表因每位医生而异.所以,我必须将它放在UICollectionView中.
我尝试过在网上找到的几种解决方案,但我仍然无法接近它.
我无法解决的最大问题是:我不知道代码中加载子数据源(医生日程安排)的位置以及何时可以加载它,因为我不能有两个函数,如下所示:
collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
这是我想要实现的目标
UIImage和医生名称(UILabel)在父UICollectionViewCell(垂直滚动)中,然后框中的所有内容(练习日n练习时间)都是子UICollectionView(水平滚动)
PS:有很多医生,每个医生都有几天练习.
请帮我怎么做
如果你真的想在collectionViewCell中插入一个collectionView,那么有一个非常简单的步骤.创建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") } }