我在尝试从 patchthecode 的教程重新创建日历时遇到一些问题,链接如下:https ://patchthecode.github.io/MainTutorial2/ 。这是我得到的一些错误:
1.无法将“ViewController”类型的值分配给“UICollectionViewDataSource”类型?
2.无法将“ViewController”类型的值分配给“UICollectionViewDelegate”类型?
3.“JTAppleCalendarView”类型的值?没有成员“registerCellViewXib”
4.“JTAppleCalendarView”类型的值?没有成员“cellInset”
5.使用未声明类型'DateCell'
6.'calendar(_:cellForItemAt:cellState:indexPath:)'的无效重新声明
7.使用未解析的标识符“myCustomCell”
import UIKit
import JTAppleCalendar
class ViewController: UIViewController {
let white = UIColor.white
let darkPurple = UIColor.purple
let dimPurple = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)
@IBOutlet weak var calendarView: JTAppleCalendarView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
calendarView.dataSource = self
calendarView.delegate = self
calendarView.registerCellViewXib(file: "CellView") // Registering your cell is manditory
calendarView.cellInset = CGPoint(x: 0, y: 0) // default is (3,3)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell: DateCell = JTAppleCell() as! DateCell
return cell
}
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
// let myCustomCell = cell as! CellView
// Setup Cell text
myCustomCell.dayLabel.text = cellState.text
// Setup text color
if cellState.dateBelongsTo == .thisMonth {
myCustomCell.dayLabel.textColor = UIColor.black
} else {
myCustomCell.dayLabel.textColor = UIColor.gray
}
}
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let myCustomCell = calendar.dequeueReusableCell(withReuseIdentifier: "CellView", for: indexPath) as! CellView
// self.calendar(self.calendar, willDisplay: myCustomCell, forItemAt: date, cellState: cellState, indexPath: indexPath)
return myCustomCell
}
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
let startDate = formatter.date(from: "2016 02 01")! // You can use date generated from a formatter
let endDate = Date() // You can also use dates created from this function
let parameters = ConfigurationParameters(startDate: startDate,
endDate: endDate,
numberOfRows: 6, // Only 1, 2, 3, & 6 are allowed
calendar: Calendar.current,
generateInDates: .forAllMonths,
generateOutDates: .tillEndOfGrid,
firstDayOfWeek: .sunday)
return parameters
}
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
let myCustomCell = cell as! CellView
// Let's make the view have rounded corners. Set corner radius to 25
myCustomCell.selectedView.layer.cornerRadius = 25
if cellState.isSelected {
myCustomCell.selectedView.isHidden = false
}
}
func calendar(_ calendar: JTAppleCalendarView, didDeselectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
let myCustomCell = cell as! CellView
myCustomCell.selectedView.isHidden = true
}
// Function to handle the text color of the calendar
func handleCellTextColor(view: JTAppleCell, cellState: CellState) {
guard let myCustomCell = view as? CellView else {
return
}
if cellState.isSelected {
myCustomCell.dayLabel.textColor = UIColor.purple
} else {
if cellState.dateBelongsTo == .thisMonth {
myCustomCell.dayLabel.textColor = UIColor.white
} else {
myCustomCell.dayLabel.textColor = UIColor.purple
}
}
}
// Function to handle the calendar selection
func handleCellSelection(view: JTAppleCell?, cellState: CellState) {
guard let myCustomCell = view as? CellView else {
return
}
if cellState.isSelected {
myCustomCell.selectedView.layer.cornerRadius = 25
myCustomCell.selectedView.isHidden = false
} else {
myCustomCell.selectedView.isHidden = true
}
}
extension UIColor {
convenience init(colorWithHexValue value: Int, alpha:CGFloat = 1.0){
self.init(
red: CGFloat((value & 0xFF0000) >> 16) / 255.0,
green: CGFloat((value & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(value & 0x0000FF) / 255.0,
alpha: alpha
)
}
}