2017-06-18 212 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

相關問題