Swift中JTAppleCalendar中日期标签下的月份名称

时间:2018-06-12 08:10:03

标签: ios swift4 jtapplecalendar

我正在尝试使用youtube上的JTAppleCalendar教程。在这个我正在制作自定义日历,在日期标签下我试图显示当前月份名称。我也将月份名称放在视图的顶部。当我传递月份名称值时,它会在视图顶部正确显示,但是当我在单元格中传递给我的标签时,它只显示标签文本而不是月份名称。我怎样才能获得月份名称?我的日历代码是:

func setUpViewForCalendar(from visibleDate: DateSegmentInfo){

    let date  = visibleDate.monthDates.first?.date
    self.formatter.dateFormat = "yyyy MM dd"
    self.yearLbl.text = self.formatter.string(from: date!)
    self.formatter.dateFormat = "MMMM"
    self.monthLbl.text = self.formatter.string(from: date!)
}
extension ViewController : JTAppleCalendarViewDataSource{
func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {

}

func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
    formatter.dateFormat = "yyyy MM dd"
    formatter.timeZone = Calendar.current.timeZone
    formatter.locale = Calendar.current.locale

    let startDate = formatter.date(from: "2018 01 01")
    let endDate = formatter.date(from: "2018 12 31")

    let parameter = ConfigurationParameters(startDate: startDate!, endDate: endDate!)
    return parameter
}

}

extension ViewController : JTAppleCalendarViewDelegate{

func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
    let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomCell
    cell.dateLbl.text = cellState.text
    cell.monthLbl.text = self.monthLbl.text
    print(cell.monthLbl.text)
    handleCellTextColor(view: cell, cellState: cellState)
    return cell
}

func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
    handleCellTextColor(view: cell!, cellState: cellState)
}

func calendar(_ calendar: JTAppleCalendarView, didDeselectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
    handleCellTextColor(view: cell!, cellState: cellState)
}

func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
    setUpViewForCalendar(from: visibleDates)
}

}

这是我日历的样子, enter image description here

1 个答案:

答案 0 :(得分:0)

let formatter = DateFormatter()
formatter.dateFormat = "MMMM"

func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
    let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomCell
    cell.dateLbl.text = cellState.text
    cell.monthLbl.text = formatter.string(from: date)
    print(cell.monthLbl.text)
    handleCellTextColor(view: cell, cellState: cellState)
    return cell
}