2017-06-18 211 views
0

我正在使用Swift为我工作的医院构建iOS应用程序。在UICollectionViewCell中添加UICollectionView

不知何故,在一个特定的功能,我不得不把UICollectionView放在UICollectionViewCell中。我想要达到的目标是父视图(垂直滚动)的每个内容都有几个子视图(可以水平滚动),这取决于父行。

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

我已经尝试了几种我在网上找到的解决方案,但直到现在我仍然无法接近它。

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

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

这是一个我想要实现

design of collection view

的UI图像和医生的名字(的UILabel)是父集合观察室(垂直滚动),和然后在盒子里的一切(练习第n天练习e时间)是儿童收集视图(水平滚动)

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

请帮助我如何做到这一点

回答

1

如果你真的想在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") 
    } 
} 

Sample screenshot

+0

中实现它我必须有两类视图控制器吗?我必须从JSON中“拉出”数据,因此我应该在哪里放入该代码?谢谢 – christ2702

+0

我已经摆脱了以前的错误,请给我一个建议,当我必须加载我的数据。 – christ2702

+0

它在模拟器上效果很好。 – Bikram

1

有多种方式来解决内部的另一个垂直列表集合的水平收集的问题。

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

This article about placing collection view inside a table view以非常复杂的方式解释了问题,并且the code for the same can be found here

+0

我已经尝试过,但我仍然没有得到它。我不知道如何在项目 – christ2702

0

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

+0

对不起,我只是不知道如何执行它 – christ2702

+0

,我不明白的是主要的问题,为什么我必须对父集合两个收集视图类或只是视图?以及如何将列表从孩子传递给父母? – christ2702

相关问题