UICollectionViewCell поведение - PullRequest
0 голосов
/ 30 апреля 2019

Я пытаюсь реализовать UICollectionView в моем UIViewController.Я использую файл Xib для UICollectionView и другой Xib для UICollectionViewCell.Я пытаюсь получить размер ячейки в соответствии с размером представления коллекции.

Моя проблема заключается в том, что когда я увеличиваю высоту представления коллекции, ячейка работает неправильно.

Неожиданное поведение

Высота по умолчанию

Код ячейки

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
    }
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...