我正在尝试使用swift 3.0 + xib创建无限日历。我有2个.xib文件:JKCalendar(collectionView)和JKCalenderCell(日期单元格)。当我在UIView上使用我的JKCalendar类时,我得到符合此视图大小的日历,但我的单元格已关闭。更有甚者,我无法使用FlowDelegate设置正确的列数。
class JKCalendarCell: UICollectionViewCell {
@IBOutlet var test: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
let view = Bundle.main.loadNibNamed("JKCalendarCell", owner: self, options: nil)?[0] as! UIView
addSubview(view)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
class JKCalendar: UIView,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
@IBOutlet var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.frame = self.bounds
self.collectionView.translatesAutoresizingMaskIntoConstraints = true
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadXib()
}
func loadXib(){
let view = UINib(nibName: "JKCalendar", bundle: nil).instantiate(withOwner: self, options: nil)[0]
addSubview(view as! UIView)
self.collectionView.register(UINib(nibName: "JKCalendarHeader" , bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "JKCalenderHeader")
self.collectionView.register(JKCalendarCell.self, forCellWithReuseIdentifier: "JKCalendarCell")
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 42
}
public func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "JKCalendarCell", for: indexPath) as! JKCalendarCell
return cell
}
public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let cell = cell as! JKCalendarCell
let dayNumber = countDayNumber()
cell.test.text = "\(dayNumber)"
cell.backgroundColor = UIColor.white
if indexPath.row % 2 == 0 {
cell.backgroundColor = UIColor.red
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = (collectionView.frame.width / 7.0 )
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
JKCalendar.xib 我的ViewContoller
我想要7个单元格/行,标签位于中心。我使用带有标签的红色单元格来显示它们已关闭了多少