UICollectionViewCell行为

时间:2019-04-29 21:06:05

标签: swift

我正在尝试在UIViewController中实现UICollectionView。 我正在将Xib文件用于UICollectionView,将另一个Xib用于UICollectionViewCell。 我正在尝试根据集合视图的大小来设置单元格的大小。

我的问题是,当我增加收藏视图的高度时,单元格无法正常运行。

How do I change my node winston JSON output to be single line

Unexpected behaviour

单元格代码

class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var backView: UIView!
@IBOutlet weak var lineView: UIView!
@IBOutlet weak var dayLabel: UILabel!
@IBOutlet weak var lineLeftView: UIView!
@IBOutlet weak var lineRightView: UIView!
@IBOutlet weak var lineRightViewFull: UIView!
@IBOutlet weak var lineLeftViewFull: UIView!

let grayColor = UIColor(red: CGFloat(33/255), green: CGFloat(33/255), blue: CGFloat(33/255), alpha: 0.1)
let grayColor_bottomLine = UIColor(red: CGFloat(188/255), green: CGFloat(188/255), blue: CGFloat(188/255), alpha: 1)


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

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

func design() {        
    let rectShape : CAShapeLayer  = CAShapeLayer()
    rectShape.bounds = backView.frame
    rectShape.position = backView.center
    rectShape.path = UIBezierPath(roundedRect: backView.bounds, byRoundingCorners: [.topLeft , .topRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath
    backView.layer.mask = rectShape

    backView.backgroundColor = grayColor
    dayLabel.textColor = grayColor
    lineRightView.backgroundColor = grayColor
    lineLeftView.backgroundColor = grayColor
    lineView.backgroundColor = grayColor

    lineRightView.isHidden = true
    lineLeftView.isHidden = true
    lineRightViewFull.isHidden = true
    lineLeftViewFull.isHidden = true
}

func dataEntry(data : CollectionViewCellData) {
    dayLabel.text = data.day
    if data.IsItFirstDay {
        lineLeftView.isHidden = false
    }

    if data.IsItWeekEnd {
        lineRightView.isHidden = false
    }
}

}

收藏夹查看代码

class CollectionViewComponent: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

var cell : CollectionViewCell = CollectionViewCell()
var data : [CollectionViewCellData] = [CollectionViewCellData]()

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! CollectionViewCell
    cell.design()
    cell.dataEntry(data:data[indexPath.row])
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    let leftRightInset : CGFloat = 0
    let topBottomInset : CGFloat = 44.5
    return UIEdgeInsets(top: topBottomInset, left: leftRightInset, bottom: topBottomInset, right: leftRightInset)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0.0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0.0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    return CGSize(width: 20, height: collectionView.frame.height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: 20, height: collectionView.frame.height)
}

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

override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
    super.init(frame: frame, collectionViewLayout: layout)
}

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

init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout, data: [CollectionViewCellData]) {
    super.init(frame: frame, collectionViewLayout: layout)
    self.data = data
    register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "Cell")
    delegate = self
    dataSource = self
    backgroundColor = UIColor.white

}

}

查看控制器代码

class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var backView: UIView!
@IBOutlet weak var lineView: UIView!
@IBOutlet weak var dayLabel: UILabel!
@IBOutlet weak var lineLeftView: UIView!
@IBOutlet weak var lineRightView: UIView!
@IBOutlet weak var lineRightViewFull: UIView!
@IBOutlet weak var lineLeftViewFull: UIView!

let grayColor = UIColor(red: CGFloat(33/255), green: CGFloat(33/255), blue: CGFloat(33/255), alpha: 0.1)
let grayColor_bottomLine = UIColor(red: CGFloat(188/255), green: CGFloat(188/255), blue: CGFloat(188/255), alpha: 1)


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

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

func design() {        
    let rectShape : CAShapeLayer  = CAShapeLayer()
    rectShape.bounds = backView.frame
    rectShape.position = backView.center
    rectShape.path = UIBezierPath(roundedRect: backView.bounds, byRoundingCorners: [.topLeft , .topRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath
    backView.layer.mask = rectShape

    backView.backgroundColor = grayColor
    dayLabel.textColor = grayColor
    lineRightView.backgroundColor = grayColor
    lineLeftView.backgroundColor = grayColor
    lineView.backgroundColor = grayColor

    lineRightView.isHidden = true
    lineLeftView.isHidden = true
    lineRightViewFull.isHidden = true
    lineLeftViewFull.isHidden = true
}

func dataEntry(data : CollectionViewCellData) {
    dayLabel.text = data.day
    if data.IsItFirstDay {
        lineLeftView.isHidden = false
    }

    if data.IsItWeekEnd {
        lineRightView.isHidden = false
    }
}

}

0 个答案:

没有答案