在UICollectionViewCell中添加UICollectionView [英] Add UICollectionView in UICollectionViewCell

查看:96
本文介绍了在UICollectionViewCell中添加UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



不知何故,在一个特定的功能中,我必须在UICollectionViewCell中放置一个UICollectionView。我想要实现的是父集合视图(垂直滚动)的每个内容都有几个子节点(可以水平滚动),具体取决于父行。



为了说明,我必须显示医生名单(姓名和照片),然后我必须在他们的名字和照片下方显示他们的每个练习时间表。根据每位医生的不同,练习时间表也会有所不同。所以,我必须把它放在集合视图中。



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

我无法解决的最大问题是:我不知道代码中加载子数据源的地方(医生时间表)和我什么时候可以加载它,因为我不能有两个类似下面的函数

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

这是我想要实现的目标





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



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



请帮我怎么做

解决方案

<如果你真的想在collectionViewCell中插入一个collectionView,那么就有一个非常简单的步骤。创建UICollectionView的实例并将其添加到collectionViewCell。如果您愿意,可以使用此示例。

  // 
// ViewController.swift
// StackOverFlowAnswer
//
//由BIKRAM BHANDARI于18/6/17创建。
//版权所有©2017 BIKRAM BHANDARI。版权所有。
//

import UIKit

class ViewController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

let cellId =CellId; //唯一的单元格ID

覆盖func viewDidLoad(){
super.viewDidLoad()
view.backgroundColor = .red; //只是为了测试

collectionView.register(Cell.self,forCellWithReuseIdentifier:cellId)//注册集合视图单元类
setupViews(); //设置所有视图
}

func setupViews(){

view.addSubview(collectionView); //添加集合视图以查看控制器
collectionView.delegate = self; // set delegate
collectionView.dataSource = self; //设置数据源

collectionView.leftAnchor.constraint(equalTo:view.leftAnchor).isActive = true; //设置集合视图的位置
collectionView.rightAnchor.constraint(equalTo:view.rightAnchor).isActive = true; //集合视图的顶部锚点
collectionView.heightAnchor.constraint(equalTo:view.heightAnchor).isActive = true; // height
collectionView.widthAnchor.constraint(equalTo:view.widthAnchor).isActive = true; // width

}

let collectionView:UICollectionView = {//要添加到视图控制器的集合视图
let cv = UICollectionView(frame:.zero, collectionViewLayout:UICollectionViewFlowLayout()); //流量布局为零大小
cv.translatesAutoresizingMaskIntoConstraints = false; //将其设置为false,以便我们可以提供约束
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;
返回单元格;
}

//行数
func collectionView(_ collectionView:UICollectionView,numberOfItemsInSection section:Int) - > Int {
返回5;
}

//每个CollecionViewCell的大小
func collectionView(_ collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:IndexPath) - > CGSize {
返回CGSize(width:view.frame.width,height:200);
}
}

//第一个UICollectionViewCell
类Cell:UICollectionViewCell,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {

let cellId =CellId ; //与上面的唯一ID相同

重写init(frame:CGRect){
super.init(frame:frame);

setupViews();
collectionView.register(UICollectionViewCell.self,forCellWithReuseIdentifier:cellId); //注册自定义UICollectionViewCell类。
//这里我没有使用任何自定义类

}


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 = .horizo​​ntal; //将滚动方向设置为水平
let cv = UICollectionView(frame:.zero,collectionViewLayout:layout);
cv.backgroundColor = .blue; //测试
cv.translatesAutoresizingMaskIntoConstraints = false;
返回cv;
}();

func collectionView(_ collectionView:UICollectionView,cellForItemAt indexPath:IndexPath) - > UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:cellId,for:indexPath);
cell.backgroundColor = .red;
返回单元格;
}

func collectionView(_ collectionView:UICollectionView,numberOfItemsInSection section:Int) - > Int {
返回5;
}

func collectionView(_ collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:IndexPath) - > CGSize {
返回CGSize(width:self.frame.width,height:self.frame.height - 10);
}
必需init?(编码器aDecoder:NSCoder){
fatalError(init(编码器:)尚未实现)
}
}


I am using Swift to build an iOS application for the Hospital I work at.

Somehow, In a specific feature I have to put an UICollectionView inside the UICollectionViewCell. The one I want to achieve was for every content of the parent collection view (vertical scrolling) would have several child (which are can be scrolled horizontal) depending from the parent row.

For illustration, I have to display list of the Doctor (name & photo) and then I have to display each of the practice schedule of them below their name and photo. The practice schedule would be variate depending of each doctor. So, I have to put it inside the collection view.

I have try several solutions that I found in the web, but till now I still could not approach it.

The most problem that I can't solve was : I don't know where is the place in the code to load the child data source (doctor schedule) and when I could load it, because I can't have two func like below

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

this is the one I want to achieve

the ui image and doctor name (uilabel) was in the parent collection view cell (scroll vertically), and then everything in the box (practice day n practice time) are the child collection view (scroll horizontally)

PS: there are many doctors, and each of the doctor has several practice day.

please help me how to do this

解决方案

If you really want to insert an collectionView inside a collectionViewCell then there is a pretty simple step. Create an instance of UICollectionView and add it the collectionViewCell. You can use this example if you like.

//
//  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")
    }
}

这篇关于在UICollectionViewCell中添加UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆