gpt4 book ai didi

swift - 滑动到其他月份时 FSCalendar 自定义问题

转载 作者:行者123 更新时间:2023-11-30 10:54:39 59 4
gpt4 key购买 nike

我正在尝试更改特定日期的填充颜色,这些日期是当前显示的月份的一部分。

我的问题:这只适用于最初显示的月份。如果我来回滑动到下个月/上个月,背景颜色似乎会分配给随机日期。

示例

最初显示的月份(按预期工作): Initially displayed month上个月(颜色分配错误):

Previous month

我的 View Controller

import Foundation
import FSCalendar

// MARK: - Class: CalendarViewController
class CalendarViewController: UIViewController, FSCalendarDelegate, FSCalendarDelegateAppearance {

// MARK: Instance Properties
fileprivate lazy var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd"
return formatter
}()

var uncessfulTrainingDays: [String] = []
var notcompletedTrainingDays: [String] = []

// MARK: IBOutlets
@IBOutlet private weak var meetingInfoView: UIView!
@IBOutlet private weak var milestoneReachedLabel: UILabel!
@IBOutlet private weak var nextMilestoneLabel: UILabel!
@IBOutlet private weak var calendar: FSCalendar!
@IBOutlet private weak var nextMeetingDateLabel: UILabel!
@IBOutlet private weak var nextMeetingDescriptionLabel: UILabel!

// MARK: Overridden Methods
override func viewDidLoad() {
super.viewDidLoad()
uncessfulTrainingDays = ["2019/01/06"]
notcompletedTrainingDays = []
meetingInfoView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.meetingPressed)))
calendar.delegate = self
calendar.select(Date())
nextMeetingDateLabel.text = "26.12.2018"
nextMeetingDescriptionLabel.text = "Meeting: check healing progress"
}

func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor? {
let currentDate = Date()
let key = self.dateFormatter.string(from: date)
let cal = Calendar.current

// Is date in the curently displayed month?
let monthOfDate = cal.component(.month, from: date)
let monthOfCal = cal.component(.month, from: calendar.currentPage)

let isInDisplayedMonth = monthOfDate == monthOfCal

if isInDisplayedMonth {
if self.uncessfulTrainingDays.contains(key) {
return UIColor(named: "DarkRed")
} else if date < currentDate {
return UIColor(named: "PrimaryAccent")
}
}
return nil
}
}

当我来回滑动到上个月/下个月时,我的 FSCalendar View 似乎无法正确更新。

你知道我的代码可能有什么问题吗?

最佳答案

这个函数解决了我的问题:

func calendarCurrentPageDidChange(_ calendar: FSCalendar) {
calendar.reloadData()
}

关于swift - 滑动到其他月份时 FSCalendar 自定义问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54092951/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com