我无法解决(并且文档中不清楚)如何向CVCalendar提供信息,以便它可以决定是否显示给定日期的点。另外,如何显示与所选日期相关的文本信息?
CVCalendar附带了一些方法,用于选择是否在给定日期显示点,并在选择日期时执行操作,但不会告诉您如何将该信息实际传递到日历。有一种简单的方法吗?
答案 0 :(得分:0)
我能想出的最优雅的方法是使用字典,以便日历可以在字典中查看日期,而不是例如迭代数组中的日期
var datesDictionary:[String:String] = ["5 November, 2016":"First item","9 November, 2016":"Second item","20 November, 2016":"Third item"]
以下完整代码。您需要在故事板中根据The documentation以及主视图控制器中的UITextView(dayTextView)设置视图和委托
import UIKit
import CVCalendar
class ViewController: UIViewController,CVCalendarMenuViewDelegate,CVCalendarViewDelegate {
@IBOutlet weak var menuView: CVCalendarMenuView!
@IBOutlet weak var calendarView: CVCalendarView!
@IBOutlet var dayTextView:UITextView!
var datesDictionary:[String:String] = ["5 November, 2016":"Service","9 November, 2016":"Change Oil","20 November, 2016":"Check brakes"]
func dotMarker(shouldShowOnDayView dayView: DayView) -> Bool{
// Look up date in dictionary
if(datesDictionary[dayView.date.commonDescription] != nil){
return true // date is in the array so draw a dot
}
return false
}
func dotMarker(colorOnDayView dayView: DayView) -> [UIColor]{
return [UIColor.black]
}
func didSelectDayView(_ dayView: DayView, animationDidFinish: Bool){
dayTextView.text = ""
// Look up date in dictionary
if(datesDictionary[dayView.date.commonDescription] != nil){
dayTextView.text = datesDictionary[dayView.date.commonDescription] // day is in the dictionary - wrote the corresponding text to dayTextView
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
menuView.commitMenuViewUpdate()
calendarView.commitCalendarViewUpdate()
}
func presentationMode() -> CalendarMode{
return .monthView
}
func firstWeekday() -> Weekday{
return .monday
}
}