3

我正在使用FSCalendar在我的项目中添加日历 UI。但我无法找到将事件添加到日历的任何解决方案。那么关于如何将事件添加到 FSCalendar 或任何第三方日历框架的任何想法?

4

4 回答 4

8

根据日期设置事件..

DateFormatter 和变量的实例:

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
}()

接下来是... DataSource 功能:

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
}

希望对您有所帮助,此代码基于 FsCalendar 示例项目。

于 2017-08-22T02:36:53.537 回答
0

试试这些代码它会帮助你

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
于 2018-05-30T06:06:44.313 回答
0

我有一个数组,其中包含事件的日期和该日期的事件数我所做的是:

1- 比较日期(使用 compareDate(data.eventDate!, toDate: date, toUnitGranularity: .Day) 比较没有时间的日期)从我的数组与日期(在函数中)

2-如果它们相等,我将在没有时间的情况下将日期添加到数组(datesWithEvent)中,以便以后可以毫无问题地进行比较

3-找到正确的日期时返回 true

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

为了准确计算该日期的事件数(不同的点数),我添加了此代码

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
}
于 2016-07-18T08:16:38.250 回答
0

在 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;
}
于 2016-06-23T07:06:52.367 回答