How to Add events to FSCalendar Swift?
Asked Answered
G

4

5

I am using FSCalendar to add calendar UI in my project . But I am unable to find any solution to add events to the calendar. So any ideas on how to add events to FSCalendar or any third party calendar framework?

Grandpa answered 17/6, 2016 at 6:12 Comment(3)
Have got any solution to add events in FSCalendar?Ananthous
Did you figure this out? I dig around in the framework and I only see code for changing colors.Contiguity
did you @Contiguity ?Melessa
H
9

To Set Events Base On Dates..

Instance of DateFormatter And Variables:

var datesWithEvent = ["2015-10-03", "2015-10-06", "2015-10-12", "2015-10-25"]

var datesWithMultipleEvents = ["2015-10-08", "2015-10-16", "2015-10-20", "2015-10-28"]

fileprivate lazy var dateFormatter2: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    return formatter
}()

next is... DataSource Function:

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {

    let dateString = self.dateFormatter2.string(from: date)

    if self.datesWithEvent.contains(dateString) {
        return 1
    }

    if self.datesWithMultipleEvents.contains(dateString) {
        return 3
    }

    return 0
}

I Hope This Help You, This Code is Base On FsCalendar Sample Project.

Headon answered 22/8, 2017 at 2:36 Comment(2)
How does one ADD an event to a date? Let's say I've got a Date() and I want to mark the date on calendarView as having an event on that date?Contiguity
Tip for super novice coders: dont forget to add "mycalendar.delegate = self" and "mycalendar.dataSource = self" to make code above work.Bengurion
E
0

Try this code,in Objective-C

- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate*)date
{
    NSString *dateString = [calendar stringFromDate:date format:@"yyyy-MM-dd"];

    if ([_datesWithEvent containsObject:dateString]){
        return 1;
    }
    else{
        NSLog(@"........Events List........");
    }
    return 0;
}
Exciting answered 23/6, 2016 at 7:6 Comment(0)
H
0

I have an array that contains events's dates and the number of event on this date what I did is:

1- Comparing dates(comparing dates without time using compareDate(data.eventDate!, toDate: date, toUnitGranularity: .Day)) from my array with date(in the function)

2- If they're equal I'll add date to an array(datesWithEvent) without time so I can compare it later without problems

3- return true when it finds the right dates

var datesWithEvent:[NSDate] = []
func calendar(calendar: FSCalendar, hasEventForDate date: NSDate) -> Bool {
    for data in eventsArray{
        let order = NSCalendar.currentCalendar().compareDate(data.eventDate!, toDate: date, toUnitGranularity: .Day)
        if order == NSComparisonResult.OrderedSame{
            let unitFlags: NSCalendarUnit = [ .Day, .Month, .Year]
            let calendar2: NSCalendar = NSCalendar.currentCalendar()
            let components: NSDateComponents = calendar2.components(unitFlags, fromDate: data.eventDate!)
            datesWithEvent.append(calendar2.dateFromComponents(components)!)
        }
    }
    return datesWithEvent.contains(date)
}

And to precise the number of event on this date (different number of dots) I've added this code

func calendar(calendar: FSCalendar, numberOfEventsForDate date: NSDate) -> Int {
    for data in eventsArray{
        let order = NSCalendar.currentCalendar().compareDate(data.eventDate!, toDate: date, toUnitGranularity: .Day)
        if order == NSComparisonResult.OrderedSame{
            return data.numberOfEvent!
        }
    }
    return 0
}
Haifa answered 18/7, 2016 at 8:16 Comment(0)
O
0

try these code it will help you

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
            let dateString = self.dateFormatter.string(from: date)
            print(dateString)
            // datesForEvents : array of dates
            if self.datesForEvents.contains(dateString) {
                return 1
            }
            return 0
Obsession answered 30/5, 2018 at 6:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.