Некоторые даты исчезают и снова появляются при прокрутке в JTCalendar Swift
Я пытаюсь настроить календарь в своем приложении с помощью библиотеки JTCalendar, но, к сожалению, у меня возникает эта проблема, когда иногда я прокручиваю приложение, месяц в представлении теряет даты, как описано на прилагаемом рисунке. Я пытался найти любую информацию или вопросы по этому поводу, но все было напрасно. Я немного новичок в этой библиотеке, может ли кто-нибудь помочь мне решить эту проблему. Спасибо за ваше время.
override func viewDidLoad() {
super.viewDidLoad()
popWithSwipe()
PLmonthArr = ["Styczeń","Luty","Marzec","Kwiecień","Maj","Czerwiec","Lipiec","Sierpień","Wrzesień","Październik","Listopad","Grudzień"]
calendarManager.menuView = calendarMenuView
calendarManager.contentView = calendarContentView
calendarManager.settings.weekDayFormat = JTCalendarWeekDayFormat.single
calendarManager.delegate = self
calendarManager.setDate(Date())
}
//MARK: View Will Appear
override func viewWillAppear(_ animated: Bool) {
currentLang = UserDefaults.standard.object(forKey:"language")as?String ?? "pl-PL"
setTextOnLanguageChange(lang:UserDefaults.standard.object(forKey:"language")as?String ?? "pl-PL")
calendarManager.reload()
}
public func calendar(_ calendar: JTCalendarManager!, prepareMenuItemView menuItemView: UIView!, date: Date!)
{
if let headerLabel = menuItemView as? UILabel{
headerLabel.textAlignment = NSTextAlignment .center
headerLabel.font = UIFont(name: "Comfortaa-Light", size: 20)
headerLabel.backgroundColor = UIColor.clear
headerLabel.textColor = UIColor.gray
let formatter = DateFormatter()
formatter.dateFormat = "MMMM yyyy"
formatter.locale = Locale(identifier: "en_US_POSIX")
let monthYear = formatter.string(from:date)
print(monthYear)
let yearFormatter = DateFormatter()
yearFormatter.dateFormat = "yyyy"
let CurrentYear = yearFormatter.string(from:date)
print(currentLang)
if currentLang == "pl-PL"{
headerLabel.text = setmonthLang(monthYear: monthYear,CurrentYear:CurrentYear).uppercased()
}else //if currentLang == "en"
{
headerLabel.text = monthYear.uppercased()
}
}
}
func numberOfMondaysInMonth(month: Int, forYear year: Int) -> Int?
{
let calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
calendar.firstWeekday = 2 // 2 == Monday
// First monday in month:
let comps = NSDateComponents()
comps.month = month
comps.year = year
comps.weekday = calendar.firstWeekday
comps.weekdayOrdinal = 1
guard let first = calendar.date(from: comps as DateComponents) else {
return nil
}
// Last monday in month:
comps.weekdayOrdinal = -1
guard let last = calendar.date(from: comps as DateComponents) else {
return nil
}
// Difference in weeks:
let weeks = calendar.components(.weekOfMonth, from: first, to: last, options: [])
return (weeks.weekOfMonth ?? 0) + 1
}
func setmonthLang(monthYear:String,CurrentYear:String)->String
{
if monthYear.contains("January")
{
return "\(PLmonthArr[0]), \(CurrentYear)"
}
else if monthYear.contains("February")
{
return "\(PLmonthArr[1]), \(CurrentYear)"
}
else if monthYear.contains("March"){
return "\(PLmonthArr[2]), \(CurrentYear)"
}
else if monthYear.contains("April"){
return "\(PLmonthArr[3]), \(CurrentYear)"
}
else if monthYear.contains("May"){
return "\(PLmonthArr[4]), \(CurrentYear)"
}
else if monthYear.contains("June"){
return "\(PLmonthArr[5]), \(CurrentYear)"
}
else if monthYear.contains("July"){
return "\(PLmonthArr[6]), \(CurrentYear)"
}
else if monthYear.contains("August"){
return "\(PLmonthArr[7]), \(CurrentYear)"
}
else if monthYear.contains("September"){
return "\(PLmonthArr[8]), \(CurrentYear)"
}
else if monthYear.contains("October"){
return "\(PLmonthArr[9]), \(CurrentYear)"
}
else if monthYear.contains("November"){
return "\(PLmonthArr[10]), \(CurrentYear)"
}
else{
return "\(PLmonthArr[11]), \(CurrentYear)"
}
}
public func calendar(_ calendar: JTCalendarManager!, prepareDayView dayView: (UIView & JTCalendarDay)!)
{
if dayView.isFromAnotherMonth(){
dayView.isHidden = true
}
let mydayview=dayView as! JTCalendarDayView
mydayview.textLabel.font = UIFont(name:"Montserrat-Regular", size:13)
mydayview.circleRatio = 2
mydayview.backgroundColor = UIColor.white
mydayview.circleView.layer.cornerRadius = 1
mydayview.textLabel.textColor = UIColor.gray
mydayview.isFromAnotherMonth = false
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateStr = dateFormatter.string(from: mydayview.date)
print(dateStr)
if(isAVailableOnSelectedDate(dateStr: dateStr)){
let now = Date()
dateFormatter.dateFormat = "yyyy-MM-dd"
print(dateFormatter.string(from: now))
if let currentDate = calendarManager.date() {
let currentDateString = String(describing: currentDate)
if dateFormatter.string(from: now) == currentDateString.prefix(10){
mydayview.circleView.isHidden = false
mydayview.circleView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)
mydayview.textLabel.textColor = UIColor.black
}
else{
mydayview.circleView.isHidden = false
mydayview.textLabel.textColor = UIColor.black
mydayview.circleView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)
}
}
}
else if calendarManager.dateHelper.date(calendarContentView.date, isTheSameMonthThan: mydayview.date)
{
mydayview.circleView.isHidden = true;
// mydayview.backgroundColor = UIColor.white
// mydayview.dotView.backgroundColor = UIColor.red
mydayview.textLabel.textColor = UIColor.gray
}
if calendarManager.dateHelper.date(Date(), isTheSameDayThan: mydayview.date){
mydayview.circleView.isHidden = false
mydayview.circleView.backgroundColor = UIColor.white
mydayview.layer.borderColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
mydayview.layer.borderWidth = 1
} else if mydayview.date > Date() || mydayview.date < Date(){
mydayview.layer.borderWidth = 0
}
}