我正在尝试以编程方式实现 JTAppleCalendar,无需 Storyboard,使用单元格的 xib 文件。我已经设置了委托(delegate)和数据源,并实现了所需的功能,但单元格没有显示。我可以看到 Collection View 已设置并在 Viewcontroller 中可见(链接 2 中图像中的蓝色方 block ),并且调用了 configureCalendar() 和 cellForItemAt(),但仍然没有显示数据。
我错过了什么?
var calendarView: JTAppleCalendarView!
override func viewDidLoad() {
super.viewDidLoad()
calendarView = JTAppleCalendarView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
calendarView.register(CalendarCellView.self, forCellWithReuseIdentifier: "cell")
calendarView.ibCalendarDelegate = self
calendarView.ibCalendarDataSource = self
self.view.addSubview(calendarView)
self.view.bringSubview(toFront: calendarView)
self.calendarView.backgroundColor = UIColor.blue
}
extension TestViewController: JTAppleCalendarViewDataSource {
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
let startDate = formatter.date(from: "2016 02 01")! // You can use date generated from a formatter
let endDate = Date() // You can also use dates created from this function
let parameters = ConfigurationParameters(startDate: startDate,
endDate: endDate,
numberOfRows: 6, // Only 1, 2, 3, & 6 are allowed
calendar: Calendar.current,
generateInDates: .forAllMonths,
generateOutDates: .tillEndOfGrid,
firstDayOfWeek: .sunday)
return parameters
}
func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
}
}
extension TestViewController: JTAppleCalendarViewDelegate {
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "cell", for: indexPath) as! CalendarCellView
cell.dayLabel?.text = cellState.text
print(cellState.text)
return cell
}
}
class CalendarCellView: JTAppleCell {
@IBOutlet var dayLabel: UILabel!
@IBOutlet var taskImage: UIImageView!
override func awakeFromNib() {
dayLabel.textColor = UIColor.red
self.backgroundColor = UIColor.blue
}
}
最佳答案
这是一种通过代码实现 JTAppleCalendar 的可能方法,可能对您有用:
注意:这只是启动和运行的起点,进一步的自定义取决于您
JTAppleCalendar 版本:8.0.3
在viewDidLoad中:
let cal = JTACMonthView(frame: CGRect.zero)
cal.backgroundColor = .white
cal.cellSize = 20
cal.calendarDelegate = self
cal.calendarDataSource = self
cal.register(DateCell.self, forCellWithReuseIdentifier: "calenderCellID")
view.addSubview(cal)
cal.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([cal.centerXAnchor.constraint(equalTo: view.centerXAnchor),
cal.topAnchor.constraint(equalTo: view.topAnchor),
cal.widthAnchor.constraint(equalToConstant: 200),
cal.heightAnchor.constraint(equalToConstant: 200)])
自定义单元格:
class DateCell: JTACDayCell {
var dateLabel : UILabel = {
let dateLabel = UILabel()
dateLabel.text = "sample"
return dateLabel
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(dateLabel)
dateLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([dateLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
dateLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
dateLabel.heightAnchor.constraint(equalToConstant: 15)])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
最后是数据源和委托(delegate)方法:
func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
let cell = calendar.dequeueReusableCell(withReuseIdentifier: calenderCellID, for: indexPath) as! DateCell
cell.dateLabel.text = cellState.text
return cell
}
func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
let startDate = formatter.date(from: "2016 02 01")!
let endDate = Date()
let parameters = ConfigurationParameters(startDate: startDate,
endDate: endDate,
numberOfRows: 6, // Only 1, 2, 3, & 6 are allowed
calendar: Calendar.current,
generateInDates: .forAllMonths,
generateOutDates: .tillEndOfGrid,
firstDayOfWeek: .sunday)
return parameters
}
func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
let cell = cell as! DateCell
cell.dateLabel.text = cellState.text
}
注:根据this :
These 2 functions should contain the same code, therefore it is wise to have a shared function to reduce code duplication. The only difference between these two functions should be the first line of code (the dequeuing code).
规定的功能是:
func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath)
func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath)
关于ios - JTAppleCalendar 以编程方式,单元格未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52000892/