I am using a JTAppleCalendar pod file in my project and when I installed the pod file I couldn't use the method JTAppleCell and I just could use JTAppleDayCell I watched this video
https://www.youtube.com/watch?v=Qd_Gc67xzlw
and this
https://www.youtube.com/watch?v=zOphH-h-qCs
Here is my codes
import UIKit
import JTAppleCalendar
class calendarViewController: UIViewController {
#IBOutlet weak var calendarCollectionView: UICollectionView!
let formatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension calendarViewController : JTAppleCalendarViewDataSource {
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
formatter.dateFormat = "yyyy MM dd"
formatter.timeZone = Calendar.current.timeZone
formatter.locale = Calendar.current.locale
let startDate = formatter.date(from: "2017 01 01")!
let endDate = formatter.date(from: "2017 12 31")!
let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate)
return parameters
}
func calendar(_calebdar : JTAppleCalendarView , cellForItemAt date : Date , cellState : CellState , indexPath : IndexPath ) -> JTAppleDayCell {
let cell = calendarCollectionView.dequeueReusableCell(withReuseIdentifier: "JTCustomCell", for: indexPath) as! JTCustomCell
cell.dateLabel.text = cellState.text
return cell
}
}
If you are only able to use JTAppleDayCell instead of JTAppleCell, then you are on the wrong version of the library. That video showed you for version 7.0
So the question I ask now is what version are you using?
put this in your podfile:
pod 'JTAppleCalendar'
Then go to your console and run pod update.
The version should say 7.0.4 (which is the latest version as of this date).
If it is saying some other version, then you're doing something wrong.
Related
for calendar i am using JTAppleCalendar pod in my project
and i am able to show current month in calendar, but i need to show current week in calendar but how
for current month showing in calendar am using below code
extension ViewController: JTAppleCalendarViewDataSource {
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
let startDate = formatter.date(from: "2010 01 01")!
let endDate = formatter.date(from: "2050 01 01")!
return ConfigurationParameters(startDate: startDate, endDate: endDate)
}
}
extension ViewController: JTAppleCalendarViewDelegate {
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "dateCell", for: indexPath) as! DateCell
self.calendar(calendar, willDisplay: cell, forItemAt: date, cellState: cellState, indexPath: indexPath)
if testCalendar.isDateInToday(date) {
cell.dateLabel.textColor = UIColor.blue
cell.dateLabel.font = UIFont.boldSystemFont(ofSize: 13.0)
} else {
testCalendar.isDateInToday(date) == false
}
return cell
}
func calendar(_ calendar: JTAppleCalendarView, headerViewForDateRange range: (start: Date, end: Date), at indexPath: IndexPath) -> JTAppleCollectionReusableView {
let header = calendar.dequeueReusableJTAppleSupplementaryView(withReuseIdentifier: "DateHeader", for: indexPath) as! DateHeader
formatter.dateFormat = "MMM yyyy"
header.monthTitle.text = formatter.string(from: range.start)
return header
}
}
0/p:
but i need to show current week in calendar, how?, plz do help
Return '1' for numberOfRows and modify the configureCalendar like this:
extension ViewController: JTAppleCalendarViewDataSource {
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
let startDate = formatter.date(from: "2010 01 01")!
let endDate = formatter.date(from: "2050 01 01")!
return ConfigurationParameters(startDate: startDate,
endDate: endDate,
numberOfRows: 1,
generateInDates: .forFirstMonthOnly,
generateOutDates: .off,
hasStrictBoundaries: false)
}
}
Following cocoapod match your requirement :
FSCalender
Please try to use below pod in pod file:
use_frameworks!
target '<Your Target Name>' do
pod 'FSCalendar'
end
import FSCalendar
import SwiftyJSON
import Alamofire
//import CalendarKit
import UIKit
struct PublicHoliday {
var name : String
var date : String
}
class ViewController: UIViewController, FSCalendarDelegate, FSCalendarDataSource {
#IBOutlet var calendar: FSCalendar!
#IBOutlet var contentsLabel: UILabel!
let formatter= DateFormatter()
var PH : [PublicHoliday]=[]
override func viewDidLoad() {
super.viewDidLoad()
calendarApi(year:"2020")
calendar.appearance.titleWeekendColor = UIColor.orange
calendar.allowsMultipleSelection = true
calendar.clipsToBounds = true
calendar.delegate = self
calendar.dataSource = self
}
func calendarApi(year:String){
let baseURLStr = "https://calendarific.com/api/v2/holidays"
let apiKey = "-------------------"
let urlStr = ["api_key":apiKey,"country":"KR","year":year]
//let urlStr = url + "api_key" +apiKey +"&country=KR"+"&year="+year
Alamofire.request(baseURLStr, method: .get, parameters: urlStr, encoding: URLEncoding.default).validate(statusCode:200..<300).responseJSON{
(response) in
switch response.result{
case .success(let value):
let jsonObject = JSON(value)
let count = jsonObject["response"]["PH"].count
for i in 0..<count {
let name = jsonObject["response"]["PH"][i]["name"].string!
let date = jsonObject["response"]["PH"][i]["date"]["iso"].string!
let ph = PublicHoliday(name: name, date: date)
self.PH.append(ph)
self.calendar.reloadData()
}
case .failure(_):
print("error")
}
}
}
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
formatter.dateFormat = "EEEE MM-dd-YYYY"
let string = formatter.string(from: date)
print("\(string)")
}
func calendar(_ calendar:FSCalendar, appearance:FSCalendarAppearance, fillDefaultColorFor date: Date)->UIColor?{
let dataForm = formatter.string(from:date)
for ph in self.PH{
if ph.date.compare(dataForm) == ComparisonResult.orderedSame{
return UIColor.cyan
}
}
return nil
}
func calendar(_ calendar: FSCalendar, subtitleFor date: Date) -> String? {
let dataForm = formatter.string(from: date)
for ph in self.PH{
if ph.date.compare(dataForm) == ComparisonResult.orderedSame{
return ph.name
}
}
return nil
}
}
In these two methods,
func calendar(_ calendar:FSCalendar, appearance:FSCalendarAppearance, fillDefaultColorFor date: Date)->UIColor?{
let dataForm = formatter.string(from:date)
for ph in self.PH{
if ph.date.compare(dataForm) == ComparisonResult.orderedSame{
return UIColor.cyan
}
}
return nil
}
func calendar(_ calendar: FSCalendar, subtitleFor date: Date) -> String? {
let dataForm = formatter.string(from: date)
for ph in self.PH{
if ph.date.compare(dataForm) == ComparisonResult.orderedSame{
return ph.name
}
}
return nil
}
You are comparing the string representations of the dates from the data source PH and the date parameter supplied by the datasource/delegate method of FSCalendar. However, you did not set a correct format (or any format at all, actually) for the formatter to use. This causes formatter.string(from:) to produce empty strings, so you are comparing empty strings with actual dates like 2020-12-25. From a brief look of the API documentation of the API that you are using, it seems like the dates come in the ISO 8601 local date format.
You should therefore, set a format like this:
func calendar(_ calendar:FSCalendar, appearance:FSCalendarAppearance, fillDefaultColorFor date: Date)->UIColor?{
formatter.dateFormat = "yyyy-MM-dd"
let dataForm = formatter.string(from:date)
for ph in self.PH{
if ph.date == dataFrom {
return UIColor.cyan
}
}
return nil
}
func calendar(_ calendar: FSCalendar, subtitleFor date: Date) -> String? {
formatter.dateFormat = "yyyy-MM-dd"
let dataForm = formatter.string(from: date)
for ph in self.PH{
if ph.date == dataFrom {
return ph.name
}
}
return nil
}
An even better approach would be to use Date in your data source directly:
struct PublicHoliday {
var name : String
var date : Date
}
And parse it when you get the response:
let dateString = jsonObject["response"]["PH"][i]["date"]["iso"].string!
formatter.dateFormat = "yyyy-MM-dd"
let date = formatter.date(from: dateString)!
let ph = PublicHoliday(name: name, date: date)
This way you can compare ph.date directly with the date supplied by FSCalendar.
I am using DaySquare calendar.
I have dateObjects array.
I have to show this dates in calendar but I am getting reason: 'Invalid month: 0' error.
import UIKit
import Daysquare
class ViewController: UIViewController {
var dateObjects = [Date]()
var calendarFromDateArr2 = [String]()
#IBOutlet var calendarView: DAYCalendarView!
override func viewDidLoad() {
super.viewDidLoad()
calendarFromDateArr2 = ["2017-10-30T07:41:00", "2017-10-30T11:23:00", "2017-10-30T11:48:00", "2017-11-10T00:00:00", "2017-11-13T19:43:00", "2017-12-01T00:00:00", "2017-12-31T00:00:00"]
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateObjects = calendarFromDateArr2.flatMap { dateFormatter.date(from: $0) }
print(dateObjects)
for date in dateObjects
{
self.calendarView.selectedDate = date
}
}
}
I am getting single value when I give "return" after this line:
self.calendarView.selectedDate = date.
This is the entire extension section of the JTAppleCalendar delegate and datasource methods implementation.
extension ViewController : JTAppleCalendarViewDelegate, JTAppleCalendarViewDataSource {
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
// set date formatter
formatter.dateFormat = "yyyy MM dd"
formatter.timeZone = Calendar.current.timeZone
formatter.locale = Calendar.current.locale
let startDate = Date()
let endDate = (Calendar.current as NSCalendar).date(byAdding: .day, value: 180, to: startDate, options: [])!
let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate)
return parameters
}
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "DateCell", for: indexPath) as! AccountsDateCell
cell.dateLabel.text = cellState.text
return cell
}
}
Xcode is saying:
Type 'ViewController' does not conform to protocol 'JTAppleCalendarViewDelegate'
It looks like the problem is with manual installation of the library. I dragged and dropped the files into this project. Is there any other way to manually add the library to the project?
You must implement the configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters delegate method.
If you are using Xcode 9, it should offer to insert the required function stubs for you.
Hi I am newbie to JTAppleCalendar. I need help.
I have installed it thru cocoapods and used some of the code as below.
I need help on how to use the basic functions such as:
Set the calendar for the current month
JTC.scrollToDate(Date())
show the current date on the calendar.
Do I need to specify or configure how many years this calendar support like 100 or 200 years?
Is this way correct to get a reference for the JTAppleCalendar
#IBOutlet weak var JTC: JTAppleCalendarView!
Please help to correct the code.
import UIKit
import JTAppleCalendar
class ViewController: UIViewController {
let formatter = DateFormatter()
#IBOutlet weak var JTC: JTAppleCalendarView!
override func viewDidLoad() {
super.viewDidLoad()
let d = Date()
var dateArr = [Date]()
dateArr.append(d)
JTC.scrollToDate(Date())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: JTAppleCalendarViewDelegate,JTAppleCalendarViewDataSource{
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
formatter.dateFormat = "yyyy MM dd"
formatter.timeZone = Calendar.current.timeZone
formatter.locale = Calendar.current.locale
let startDate = formatter.date(from: "2017 01 01")!
let endDate = formatter.date(from: "2117 12 31")!
let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate)
return parameters
}
func calendar(_ calendar:JTAppleCalendarView,cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell{
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.dateLabel.text = cellState.text
return cell
}
}
Thanks
The issue was solved for me by adding three methods in ViewDidLoad and setting start and end dates as follows:
override func viewDidLoad() {
self.calendarView.visibleDates {[unowned self] (visibleDates: DateSegmentInfo) in
self.setupViewsOfCalendar(from: visibleDates)
}
self.calendarView.scrollToDate(Date(),animateScroll: false)
self.calendarView.selectDates([ Date() ])
}
In Calendar configuration set dates as follows:
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
formatter.dateFormat = "yyy MM dd"
formatter.timeZone = Calendar.current.timeZone
formatter.locale = Calendar.current.locale
var dateComponent = DateComponents()
dateComponent.year = 1
let startDate = Date()
let endDate = Calendar.current.date(byAdding: dateComponent, to: startDate)
let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate!, numberOfRows: 6, calendar: Calendar.current, generateInDates: .forFirstMonthOnly, generateOutDates: .off, firstDayOfWeek: .sunday, hasStrictBoundaries: true)
return parameters
}