Добавление числа n для представления коллекции в UIStackView в swift - PullRequest
0 голосов
/ 15 мая 2018

У меня есть класс ViewController, в который я помещаю scrollView и добавляю stackView в качестве подпредставления.Теперь я хочу добавить 'n' число collectionView внутри stackView.Я получаю данные с сервера, будут разные категории.Если у категории А есть данные, то мне нужно создать один collectionView для этого.И мне нужно обработать данные соответствующих collectionView в их классе.

class ViewController: UIViewController {

var scrollView : UIScrollView!
var stackView : UIStackView!
var responseFullData : JsonFullDataResponse!
var responseData : JsonResponse!

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    print("View Controller init")
    super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func viewDidAppear(_ animated: Bool) {
    print("View did appear")
    scrollView.contentSize = CGSize(width: stackView.frame.width, height: stackView.frame.height)
}

override func viewDidLoad() {
    super.viewDidLoad()

    //loading all the data
    loadAllJsonData()

    //setting up scroll and stack views
    setUpScrollAndStackView()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    print("inside layout")
    scrollView.contentSize = CGSize(width: stackView.frame.width, height: stackView.frame.height)
}

func setUpScrollAndStackView() {
    scrollView = UIScrollView()
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(scrollView)

    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", options: .alignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", options: .alignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))

    stackView = UIStackView()
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.spacing = 20.0
    stackView.axis = .vertical
    scrollView.addSubview(stackView)


  scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[stackView]|", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["stackView": stackView]))
  scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[stackView]", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["stackView": stackView]))

    setUpOtherViewsInStack()
}

func setUpOtherViewsInStack() {
    if responseFullData.banner?.count != 0 {
        print("banner has data")

        var bannerCollection : BannerCollectionView = BannerCollectionView() as! BannerCollectionView
        bannerCollection.register(UINib.init(nibName: "BannerCell", bundle: nil), forCellWithReuseIdentifier: "BannerCell")
        stackView.addArrangedSubview(bannerCollection)
   }
}

Код BannerCollectionView

class BannerCollectionView: UICollectionView , UICollectionViewDataSource{

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        print("control is in datasource")
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BannerCell", for: indexPath) as! BannerCell
        cell.backgroundColor = UIColor.blue
        return cell
    }
}

Как мне сделать эту работу.В каком классе и где я должен установить свойство delegate и datasource для BannerCollectionView?Как мне это инициализировать?

Ответы [ 2 ]

0 голосов
/ 31 мая 2018

Я добавил 3 collectionView внутри UIStackView (вертикальный стек).Весь вид можно прокручивать вертикально, а элементы коллекции можно прокручивать по горизонтали или по вертикали.

class HomePageViewController: UIViewController,HeaderViewDelegate {

var scrollView : UIScrollView!
var stackView : UIStackView!
var responseFullData : JsonFullDataResponse!

if responseFullData?.menu?.count != nil{
        print("menu has data")

        var menuCollection : MenuCollectionView = MenuCollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())
        menuCollection.backgroundColor = .white
        menuCollection.menuData = (self.responseFullData.menu)!
        menuCollection.dataSource = menuCollection.self
        menuCollection.delegate = menuCollection.self
        createCollectionViews(collectionViewClass: menuCollection, identifier: "MenuCell",height : 175)
    }
    if responseFullData?.hongbei?.count != nil {
        print("hongbei has data")
        var hongbeiCollection : HongbeiCollectionView = HongbeiCollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())

        hongbeiCollection.dataSource = hongbeiCollection
        hongbeiCollection.delegate = hongbeiCollection
        hongbeiCollection.allowsSelection = true
        hongbeiCollection.backgroundColor = .white
        hongbeiCollection.hongbeiData = (self.responseFullData.hongbei)!


        addHeaderView(headerTitle : "Hongbei")
        createCollectionViews(collectionViewClass: hongbeiCollection, identifier: "HongbeiCell",height : 150)

    }
    if responseFullData?.freeZone?.count != nil {
        print("freezone has data")
        var freezoneCollection : FreeZoneCollectionView = FreeZoneCollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())
        freezoneCollection.dataSource = freezoneCollection.self
        freezoneCollection.delegate = freezoneCollection.self
        freezoneCollection.backgroundColor = .white
        freezoneCollection.freeZoneData = (self.responseFullData.freeZone)!
        addHeaderView(headerTitle : "FreeZone")
        createCollectionViews(collectionViewClass: freezoneCollection, identifier: "FreeZoneCell",height : 150)
    }

func createCollectionViews(collectionViewClass : UICollectionView, identifier : String,height : CGFloat ){

    collectionViewClass.widthAnchor.constraint(equalToConstant: self.view.frame.width).isActive = true
    collectionViewClass.heightAnchor.constraint(equalToConstant: height).isActive = true
    collectionViewClass.register(UINib.init(nibName: identifier, bundle: nil), forCellWithReuseIdentifier: identifier)
    collectionViewClass.showsHorizontalScrollIndicator = false
    collectionViewClass.showsVerticalScrollIndicator = false

    stackView.addArrangedSubview(collectionViewClass)

}

 func addHeaderView(headerTitle : String){
    let headerView = HeaderView.instanceFromNib() as! HeaderView
    headerView.widthAnchor.constraint(equalToConstant: self.view.frame.width).isActive = true
    headerView.heightAnchor.constraint(equalToConstant: 20).isActive = true
    headerView.headerLabel.text = headerTitle
    headerView.frame = headerView.frame.offsetBy(dx: 20, dy: 0)
    headerView.delegate = self
    stackView.addArrangedSubview(headerView)

}

}

class MenuCollectionView: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

var menuData : [MenuData]!

override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    super.init(frame: frame, collectionViewLayout: layout)

}

   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    print("Menu")
    return menuData.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MenuCell", for: indexPath) as! MenuCell

    cell.menuLabel.text = menuData[indexPath.item].title!

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
    return CGSize(width: (collectionView.frame.width * 0.175), height: 75)
}

У меня есть отдельный класс для каждого collectionView в стеке, где я работаю с источником данных и методами делегата.Таким образом, у меня может быть любое число для элементов коллекции, которые я хочу в каждом collectionView.

0 голосов
/ 15 мая 2018

Как я понимаю, вам нужно сделать для от 0 до responseFullData.banner.count, добавляя collectionView каждый раз

if let count = responseFullData.banner?.count {
    for i in 0..<count {
        var bannerCollection : BannerCollectionView = BannerCollectionView() as! BannerCollectionView
        bannerCollection.register(UINib.init(nibName: "BannerCell", bundle: nil), forCellWithReuseIdentifier: "BannerCell")
        stackView.addArrangedSubview(bannerCollection)
    }
}
...