更改约束的高度值时出错

时间:2019-04-26 05:15:38

标签: swift xcode nslayoutconstraint

在我的代码中,我正在更改通过Ib插座“ collectionViewHeightConstraint”链接的高度约束

每当我运行应用程序时,都会弹出错误,指出存在冲突的约束。我试图打电话给setNeedsUpdateConstraints(),但并没有真正改变。

具有讽刺意味的是,仅在初始化前三个单元格时才调用错误。但是,如果我取出setNeedsUpdateConstraints(),则每当滚动表格视图时都会弹出错误消息。

class MonthWeekTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var monthLabel: UILabel!
@IBOutlet weak var dayCollectionView: UICollectionView!

@IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!

var monthYear: (String,String) = ("","") {
    didSet {

        monthLabel.text = self.monthYear.0

        guard let _maxDate = DateFormat.returnNumberOfDaysInMonth(month: self.monthYear.0, year: self.monthYear.1) else {fatalError("Tuple Error")}
        maxDate = _maxDate

        firstDay = DateFormat.getFirstOfMonth(year: self.monthYear.1, month: self.monthYear.0)

        let extra = maxDate - 36 + firstDay

        if extra > 0 {
            extraCalendarItem = extra
        } else if extra <= -7 {
            setNeedsUpdateConstraints()
            collectionViewHeightConstraint.constant = 270

        }
    }
}
var firstDay: Int = 0 {
    didSet {

        dayCollectionView.reloadData()
    }
}

var extraCalendarItem: Int = 0 {
    didSet {

        setNeedsUpdateConstraints()
        collectionViewHeightConstraint.constant = 270
    }
}

var dayNumber: Int = 1

var maxDate: Int = 30

override func awakeFromNib() {
    super.awakeFromNib()

    dayCollectionView.delegate = self
    dayCollectionView.dataSource = self
    dayCollectionView.register(UINib(nibName: "DayCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "DayCollectionViewCell")
    dayCollectionView.translatesAutoresizingMaskIntoConstraints = false

}

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

    if extraCalendarItem > 0 {
        return 6
    }

    return 5
}

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

    if section == 5 {
        return extraCalendarItem
    }
    return 7
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

    let _cell = cell as! DayCollectionViewCell
    if (indexPath.section == 0 && firstDay > indexPath.row + 1) || dayNumber > maxDate{
        _cell.isHidden = true
    } else {
        _cell.dayLabel.text = String(dayNumber)
        dayNumber += 1
        _cell.isHidden = false
    }
}


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

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


    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: collectionView.frame.width / 7, height: 45)
}

override func prepareForReuse() {

    dayNumber = 1
    extraCalendarItem = 0

    updateConstraints()
}

override func updateConstraints() {

    collectionViewHeightConstraint.constant = 225
    super .updateConstraints()
}

}

0 个答案:

没有答案
相关问题