How to show Current Week in JTAppleCalendar in swift - ios

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

Related

extension Date { has no effect

This is my original timestamp code
How should I modify to cite him
How should I modify the code to refer to the "extension Date {" code
func getDatas(){
if let timestamp = document.get("timestamp") as? TimeInterval {
let date = Date(timeIntervalSince1970: timestamp)
let post = Post(email: email, caption: caption, imageUrl: imageURL, date: date)
...
...
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FeedCell
let post = self.postArray[indexPath.row]
let date = post.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd HH:mm"
let dateString = dateFormatter.string(from: date)
self.getUserInfo(userEmail: postArray[indexPath.row].email,cell: cell)
cell.captionLabel.text = postArray[indexPath.row].caption
cell.postImage.sd_setImage(with: URL(string: postArray[indexPath.row].imageUrl))
cell.timeLabel.text = dateString
I want to change to the following date display method, but the changes have no effect
extension Date {
static func timeString(timeInterval: TimeInterval) -> String{
let date = getNowDateFromatAnDate(Date(timeIntervalSince1970: timeInterval/1000))
let formatter = DateFormatter()
if date.isToday() {
//是今天
formatter.dateFormat = "今天HH:mm"
return formatter.string(from: date)
}else if date.isYesterday(){
//是昨天
formatter.dateFormat = "昨天HH:mm"
return formatter.string(from: date)
}else if date.isSameWeek(){
//是同一周
let week = date.weekdayStringFromDate()
formatter.dateFormat = "\(week)HH:mm"
return formatter.string(from: date)
}else{
formatter.dateFormat = "yyyy-MM-dd HH:mm"
return formatter.string(from: date)
}
}
...
...
...
...
How should I modify to cite him
The static keyword allow us to attach the method to a class/struct rather than to instances of it.
And since the static func timeString(timeInterval: TimeInterval) -> String in your Date extension returns a string you can replace your Post date: Date variable with timeString: String and then you can access it directly:
if let timestamp = document.get("timestamp") as? TimeInterval {
let timeString = Date.timeString(timeInterval: timestamp)
let post = Post(email: email, caption: caption, imageUrl: imageURL, timeString: timeString)
// ...
}
cell.timeLabel.text = post.timeString

how to remake the beginning of the week, not from Sunday, but from Monday

I use the library to display the calendar, I would like to make the week start on Monday and the calendar is displayed from the current date by 2017
https://github.com/miraan/CalendarDateRangePickerViewController
Image
changed the locale on russian language, but did not help
func getMonthLabel(date: Date) -> String {
var dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ru_RU")
dateFormatter.dateFormat = "MMMM yyyy"
return dateFormatter.string(from: date)
}
func getWeekdayLabel(weekday: Int) -> String {
var components = DateComponents()
components.calendar = Calendar.current
components.weekday = weekday
let date = Calendar.current.nextDate(after: Date(), matching: components, matchingPolicy: Calendar.MatchingPolicy.strict)
if date == nil {
return "E"
}
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ru_RU")
dateFormatter.dateFormat = "EEEEE"
return dateFormatter.string(from: date!)
}
The problem was in this part of the code, corrected by 2 and managed to get the desired result.
override public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseIdentifier, for: indexPath) as! CalendarDateRangePickerCell
cell.reset()
let blankItems = getWeekday(date: getFirstDateForSection(section: indexPath.section)) - 1
if indexPath.item < 7 {
cell.label.text = getWeekdayLabel(weekday: indexPath.item **+ 2** )
} else if indexPath.item < 7 + blankItems {
cell.label.text = ""

iOS swift: selecting a cell I want to select the entire row

I have a calendar, and I want to select an entire row (week) when I select a day.. this is my code so far:
//When a date is selected
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
selectTheWeek(of: date)
setupLayoutCell(cell: cell, cellState: cellState)
}
func selectTheWeek(of date: Date) {
let starOfTheWeek = date.startOfWeek()
let endOfTheWeeK = date.endOfWeek()
calendarCollectionView.selectDates(from: starOfTheWeek, to: endOfTheWeeK)
}
extension Date {
func startOfWeek() -> Date {
let calendar = Calendar.autoupdatingCurrent
let currentDateComponents = calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)
return calendar.date(from: currentDateComponents)!
}
func endOfWeek() -> Date {
let cal = Calendar.autoupdatingCurrent
var component = DateComponents()
component.weekOfYear = 1
component.day = -1
return cal.date(byAdding: component, to: startOfWeek())!
}
}
the problem is that I'm having an infinite loop, and it's clear the reason.
But I don't know how to prevent it. Any help?
JTAppleCalender is an external library. It's an extension of a collectionView.
You can use 2 techniques to break the loop.
First:
calendarViewselectDates(from: starOfTheWeek, to: endOfTheWeeK, triggerSelectionDelegate: false)
By setting triggerSelectionDelegate to false, your delegate function didSelect will not be called.
Second:
If you are using MasterBranch code (which i'll be releasing in a week or so), you can know whether or not your selection is programmer initiated vs user initiated. You know this by --> cellState.
if cellState.selectionType == .programatic {
// ignore stuff
} else {
// Do stuff
}
You can put this if statement in your shouldSelect function.
I know this is a not the best possible solution but it could work to avoid your issue
//When a date is selected
var shouldSelectWeek = true
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
if shouldSelectWeek{
selectTheWeek(of: date)
shouldSelectWeek = false
}
setupLayoutCell(cell: cell, cellState: cellState)
}
func selectTheWeek(of date: Date) {
let starOfTheWeek = date.startOfWeek()
let endOfTheWeeK = date.endOfWeek()
calendarCollectionView.selectDates(from: starOfTheWeek, to: endOfTheWeeK)
}
extension Date {
func startOfWeek() -> Date {
let calendar = Calendar.autoupdatingCurrent
let currentDateComponents = calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)
return calendar.date(from: currentDateComponents)!
}
func endOfWeek() -> Date {
let cal = Calendar.autoupdatingCurrent
var component = DateComponents()
component.weekOfYear = 1
component.day = -1
return cal.date(byAdding: component, to: startOfWeek())!
}
Just add a new boolean variable like var shouldIgnoreDateSelection = false and then you just do
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
if shouldIgnoreDateSelection == false {
selectTheWeek(of: date)
}
setupLayoutCell(cell: cell, cellState: cellState)
}
func selectTheWeek(of date: Date) {
let starOfTheWeek = date.startOfWeek()
let endOfTheWeeK = date.endOfWeek()
shouldIgnoreDateSelection = true
calendarCollectionView.selectDates(from: starOfTheWeek, to: endOfTheWeeK)
shouldIgnoreDateSelection = false
}

Getting the "does not conform to JTAppleCalendarViewDelegate" error even when delegate method implemented

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.

JTAppleCalendar How to set current date and current month

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
}

Resources