1

我正在尝试来自 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)
}

}

我的日历是这样的 在此处输入图像描述

4

1 回答 1

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
}
于 2019-09-16T20:08:01.730 回答