I am trying to implement JTAppleCalendar programmatically without storyboards, using a xib file for the cell. I have set the delegate and datasource, and implemented the required functions, but the cells are not showing. I can see that the collection view is set up and visible in the Viewcontroller(the blue square in image in link 2), and the configureCalendar() and cellForItemAt() are called, but still no data is shown.
What am I missing?
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
}
}
JTAppleCalendarView
's super class is UICollectionView
and it uses UICollectionViewFlowLayout
to layout the items.JTAppleCalendarView
provides CellSize
to configure the item size and you also can use UICollectionViewFlowLayout
to configure Min Spacing
.
I guess you were using xib to Custom Cell, so you have to use register(_ nib: UINib?, forCellWithReuseIdentifier identifier: String)
to register the Cell.Finally, don't forget to add constraints for the dayLabel.
calendarView's Cell Size is 40, width is 280(7 columns) and height is 240(six rows), Min Spacing for cells and lines are 0.