У меня возникли проблемы при попытке воссоздать календарь из учебника patchthecode. Вот ссылка: https://patchthecode.github.io/MainTutorial2/.
Вот несколько ошибок, которые я получаю:
1.Не можете присвоить значение типа 'ViewController' типу 'UICollectionViewDataSource?'
2.Не можете присвоить значение типа 'ViewController' типу 'UICollectionViewDelegate?'
3.Значение типа 'JTAppleCalendarView?' У пользователя нет члена 'registerCellViewXib'
4. Значение типа 'JTAppleCalendarView?' не имеет члена 'cellInset'
5.Использование необъявленного типа 'DateCell'
6. Неправильная переопределение 'календаря (_: 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
)
}
}