UIColectionView里面有不同的UIViews

时间:2018-03-11 22:37:26

标签: ios swift uicollectionview

我很擅长使用swift开发应用程序,而我正在尝试在内部创建一个UICollectionView UIViews,因此我可以渲染不同的图表,希望能够实现like this

但是我创建了UICollectionView并使用Storyboards和代码显示它。以下代码显示数据而不尝试呈现图表:

import UIKit

class MoodChartsViewController: UIViewController, 
UICollectionViewDelegate, UICollectionViewDataSource {

let locationNames = ["Monthly Mood Overview", "Mood Count", "Average Daily Mood", "Activity Count", "Often Together"]

let locationDescription = ["A count of all the moods in the month of ", "A count of all the moods tracked for the month of ", "Your average daily mood.", "An activity count for the month of ", "Moods which are often together. use this as insight to improve your mood"]

override func viewDidLoad() {
}

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

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}


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

    cell.ChartName.text = locationNames[indexPath.row]
    cell.ChartDescription.text = locationDescription[indexPath.row]

    //This creates the shadows and modifies the cards a little bit
    cell.contentView.layer.cornerRadius = 4.0
    cell.contentView.layer.borderWidth = 1.0
    cell.contentView.layer.borderColor = UIColor.clear.cgColor
    cell.contentView.layer.masksToBounds = false
    cell.layer.shadowColor = UIColor.gray.cgColor
    cell.layer.shadowOffset = CGSize(width: 0, height: 1.0)
    cell.layer.shadowRadius = 4.0
    cell.layer.shadowOpacity = 1.0
    cell.layer.masksToBounds = false
    cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius).cgPath

    return cell
}
}

我的故事板看起来像this.

我认为可用于在图表中呈现UIViews的代码示例如下:

let chartViews = [UIView(ChartView), UIView(ChartView), UIView(ChartView), UIView(ChartView), UIView(ChartView)]

使用UIView类呈现XIB文件(只是为了在屏幕上显示内容):

import UIKit

class ChartView: UIView {

@IBOutlet var view: UIView!

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    UINib(nibName: "monthlyviewcount", bundle: nil).instantiate(withOwner: self, options: nil)
    addSubview(view)
    view.frame = self.bounds
}
}

但会出现此错误消息:

  

参数标签'(_ :)'与任何可用的重载都不匹配

我想我可能会过度思考这一点但是他们更容易实现这个目标吗?或者我只是遗漏了一些简单的东西?

0 个答案:

没有答案