FSCalender活动快速展示

时间:2017-08-24 08:01:36

标签: ios swift nscalendar eventkit fscalendar

我是swift的新手,我快速使用Fscalender工作正常,但我想将事件添加到Fscalender,我可以从Json获取事件

enter image description here

我想要在日历中显示事件,我可以尝试一些代码,但它无法正常运行,请帮助我们了解如何在日历中显示事件

   var EventsData = [Event]()

  all events are stored Into Event


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



        return formatter
    }()

在Json里面

        if let event_list = jsonData["events"] as? NSArray {

                                for i in 0 ..< event_list.count {

                                    if let event = event_list[i] as? NSDictionary {


                                       let data = event["date"]as?String

                                        let newString = data?.replacingOccurrences(of: "/", with: "-")

                                        print("new string data ",newString as Any)

                                       self.compareDate(date: newString!)

                                        self.EventsData.append(Event(
                                            eventId: event["eventId"] as? String,
                                            eventName:event["details"] as? String,
                                            //eventDate: event["date"] as? String
                                             eventDate: newString ))
                                    }


                                }
 }

显示活动

         func compareDate(date : String){
                let date = date

                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
                let dateFromString : NSDate = dateFormatter.date(from: date)! as NSDate

 ===>>   After this line showing Fatal error 

                dateFormatter.dateFormat = "yyyy-MM-dd"
                let datenew = dateFormatter.string(from: dateFromString as Date)
                print("datee",datenew)

            }


            func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at position: FSCalendarMonthPosition) {

                let dateFormatter3 = DateFormatter()

                dateFormatter3.dateFormat = "yyyy-MM"
                let dateString3 = dateFormatter3.string(from: date)

                //print("datenew1",dateString3)


                strcond  = dateString3 as NSString

                print("datenew1",strcond!)

            }

            func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
                let dateString = self.dateFormatter2.string(from: date)

                for d in EventsData{
                    let date = d.eventDate


                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
                    let dateFromString : NSDate = dateFormatter.date(from: date!)! as NSDate
                    dateFormatter.dateFormat = "yyyy-MM-dd"
                    let datenew = dateFormatter.string(from: dateFromString as Date)
                    if datenew.contains(dateString) {
                        return 1
                    }
                }
                return 0

            }

            func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventColorFor date: Date) -> UIColor? {
                let dateString = self.dateFormatter2.string(from: date)


                for d in EventsData{
                  let date = d.eventDate

                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
                    let dateFromString : NSDate = dateFormatter.date(from: date!)! as NSDate
                    dateFormatter.dateFormat = "yyyy-MM-dd"
                    let datenew = dateFormatter.string(from: dateFromString as Date)


                    print("new  calendar",dateString)


                    if datenew.contains(dateString) {
                        return UIColor.purple
                    }
                }
                return nil
            }


            func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
                if monthPosition == .previous || monthPosition == .next {
                    calendar.setCurrentPage(date, animated: true)

                    print("title date",date)
                }
            }

如何在日历中显示活动?

1 个答案:

答案 0 :(得分:1)

在我的代码中进行了一些更改后,它在FsCalender

中正常显示事件

enter image description here

  inside  Json



          if let event_list = jsonData["events"] as? NSArray {

                                        for i in 0 ..< event_list.count {
                                            if let event = event_list[i] as? NSDictionary {
                                                self.EventsData.append(Event(
                                                    eventId: event["eventId"] as? String,
                                                    eventName:event["details"] as? String,
                                                    eventDate: event["date"] as? String
                                                    )
                                                )
                                            }

                                        }
                                        self.do_refresh()
                                    }


     func do_refresh()
        {
            DispatchQueue.main.async(execute: {

                self.calender.reloadData()

                return
            })
        }


    Fscalender Implementation 

    func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at position: FSCalendarMonthPosition) {

            let dateFormatter3 = DateFormatter()

            dateFormatter3.dateFormat = "yyyy-MM-dd"
            let dateString3 = dateFormatter3.string(from: date)

            strcond  = dateString3 as NSString
        }


        func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
            let dateString = self.dateFormatter2.string(from: date)
            print("this count first ",self.EventsData.count)
            for d in EventsData{

                let date = d.eventDate
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "MM-dd-yyyy"
                let dateFromString : NSDate = dateFormatter.date(from: date!)! as NSDate
                dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
                let datenew = dateFormatter.string(from: dateFromString as Date)
                if datenew.contains(dateString) {
                    return 3
                }
            }
            return 0

        }

        func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventColorFor date: Date) -> UIColor? {
            let dateString = self.dateFormatter2.string(from: date)
            for d in EventsData{
                let date = d.eventDate
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "MM-dd-yyyy"
                let dateFromString : NSDate = dateFormatter.date(from: date!)! as NSDate
                dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
                let datenew = dateFormatter.string(from: dateFromString as Date)


                if datenew.contains(dateString) {
                    return UIColor.init(red: 10, green: 200, blue: 399, alpha: 300)
                }
            }
            return nil
        }