我正在使用FSCalendar在我的项目中添加日历UI。但我无法找到任何向日历添加事件的解决方案。那么有关如何向FSCalendar或任何第三方日历框架添加事件的任何想法?
答案 0 :(得分:5)
根据日期设置事件..
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示例项目。
答案 1 :(得分: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;
}
答案 2 :(得分: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
}
答案 3 :(得分: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