Bhagyalaxmi Poojary
Bhagyalaxmi Poojary

Reputation: 1213

How to Add events to FSCalendar Swift?

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?

Upvotes: 5

Views: 12041

Answers (4)

Joseph Mikko Manoza
Joseph Mikko Manoza

Reputation: 296

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.

Upvotes: 9

Musavir Parambil
Musavir Parambil

Reputation: 120

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

Upvotes: 0

Preetha
Preetha

Reputation: 749

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;
}

Upvotes: 0

Mireille
Mireille

Reputation: 677

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
}

Upvotes: 0

Related Questions