在collectionView的两个不同单元格中显示highchart和swiftDataTable

时间:2018-11-28 08:19:57

标签: swift highcharts uiview uicollectionview uicollectionviewcell

我正在尝试为每个报告显示第一个collectionViewCell中的关联图表(使用HighChart),当您向右滑动时,您将在第二个collectionViewCell中获得关联的dataTable(i使用SwiftDataTables)。最近三天面临的问题是,我无法使用

在collectionviewCell中显示任何一个

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

这是我的代码:

1- collectionView和customCell实例:

lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = .white
        cv.dataSource = self
        cv.delegate = self
        cv.isPagingEnabled = true

        return cv
    }()

var pages: [Page] = []

2-Custom CollectionViewCell:PageCell

import UIKit
import Highcharts
import SwiftDataTables
import ChameleonFramework

class PageCell: UICollectionViewCell {

    var imageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        iv.backgroundColor = .yellow
        iv.image = UIImage(named: "background_2")
        iv.clipsToBounds = true
        return iv
    }()

    var pageView: UIView= {
        let pv = UIView()
        pv.contentMode = .scaleAspectFill
        pv.clipsToBounds = true

        return pv
    }()


    var page: Page? {
        didSet {
           // print(page?.mainView)

            guard let unwrappedPage = page else {
                return
            }
            //imageView.image = UIImage(named: unwrappedPage.image)//unwrappedPage.image

            pageView = unwrappedPage.mainView
            imageView.image = UIImage(named: unwrappedPage.image)

            print("imageView.image: ", imageView.image)
            print("PageViewContent: ", pageView)
            print("unwrappedPage.mainView: ", unwrappedPage.mainView)
        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)

        setupViews()
    }

    func setupViews() {

         addSubview(imageView)
         imageView.anchorWithConstantsToTop(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, topConstant: 70, leftConstant: 0, bottomConstant: 100, rightConstant: 0)



        addSubview(pageView)
        pageView.anchorWithConstantsToTop(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, topConstant: 70, leftConstant: 0, bottomConstant: 100, rightConstant: 0)


        //imageView.frame = CGRect(x: UIScreen.main.bounds.origin.x, y: UIScreen.main.bounds.origin.y+70, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height-100)



    }

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

}

3-自定义单元格的结构:页面

import Foundation

struct Page {
    let mainView: UIView
    let image: String
}

2-将collectionView添加到View +设置约束

 //TODO: collectionView Config
  self.view.addSubview(self.collectionView)
  self.collectionView.anchorToTop(top: self.view.topAnchor, left: 
  self.view.leftAnchor, bottom: self.view.bottomAnchor, right: 
  self.view.rightAnchor)
  self.collectionView.register(PageCell.self, forCellWithReuseIdentifier: "cellId")
 self.pages = {
          let firstPage = Page(mainView: self.chartView, image: "background_2")
          let secondPage = Page(mainView: self.chartView, image: "background_2")
           return [firstPage, secondPage]
                    }()

3-CollectionView方法:

    //MARK: - UICollectionViewController Extension
extension SecondViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {



    //TODO: numberOfItemsInSection method
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pages.count
    }


    //TODO: cellForItemAt method
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! PageCell

        let page = pages[indexPath.item]

        cell.page = page

        return cell
    }

    //TODO: collectionViewLayout method

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


    //TODO: scrollViewWillEndDragging method
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

        let x = targetContentOffset.pointee.x

        pageControl.currentPage = Int(x / view.frame.width)


    }


}

知道对于imageView情况,它可以完美工作,我将每个图像保存在关联的单元格中,但是对于视图(chatyView和datatable),除非我使用:: p在集合视图之后添加它们,否则它是一个空格。

self.collectionView.addSubview(self.chartView)

self.chartView.anchorWithConstantsToTop(top: self.view.topAnchor, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 50, leftConstant: 0, bottomConstant: 20, rightConstant: 0)  


self.collectionView.addSubview(self.dataTable) 

self.dataTable.anchorWithConstantsToTop(top: self.view.topAnchor, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 70, leftConstant: 0, bottomConstant: 20, rightConstant: 0)

这是唯一可行的情况,但由于它们显示在每个单元格的另一个顶部,所以它不能满足我的需求:

到目前为止,我已经尝试过但没有为我工作:

  • 不是使用PageCell作为自定义单元格,而是使用UICollectionViewCell,并且通过使用append创建了UIView数组,我添加了视图并尝试使用itemForCellAtIndexpath方法将其分配给适当的单元格。工作。

  • 我不是使用cell = page [indexPath.item],而是使用cell.contanView.addSubView()。也没有幸免。

  • 还尝试使用UIView的Arrray位置来使用特定类型,例如HiChartView / SwiftDatable。但没有用。

我发现奇怪的是,它与UIImageView完美配合,但不适用于图表或dataTable,除非我在collectionView config之后立即添加它们。

预先感谢(y)

1 个答案:

答案 0 :(得分:1)

我通过删除来解决了这个问题:

self.dataTable.anchorWithConstantsToTop(top: self.view.topAnchor, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 70, leftConstant: 0, bottomConstant: 20, rightConstant: 0)

,而不是添加:

self.dataTable.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: self.frame.size.height)

与chartView相同。