如何比较JSON APIdate与FSCalendar和在tableview中显示事件?
我已经在日历中显示事件点,但不能显示在tableview中的月份相同的事件,我想显示这些事件,如果我们改变月和tableview数据事件应该改变,我很困惑如何做到这一点。 我使用FSCalendar来显示点和tableview中的事件。
import UIKit import Alamofire import SwiftyJSON import ToastSwift class CalendarViewController: UIViewController , UITableViewDataSource, UITableViewDelegate, FSCalendarDataSource, FSCalendarDelegate , FSCalendarDelegateAppearance{ @IBOutlet weak var tableview: UITableView! @IBOutlet weak var menu: UIBarButtonItem! @IBOutlet weak var calendar: FSCalendar! var strcond : NSString? var calendarEvents : [Calendars] = [] fileprivate lazy var dateFormatter2: DateFormatter = { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" return formatter }() override func viewDidLoad() { super.viewDidLoad() menu.target = self.revealViewController() menu.action = #selector(SWRevealViewController.revealToggle(_:)) if self.revealViewController() != nil { self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer()) } getCalendarEvents() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewWillAppear(_ animated: Bool) { } override func viewDidAppear(_ animated: Bool) { } func getCalendarEvents(){ let url = NSURL(string: "http://******/api/academics/getEvents") var request = URLRequest(url: url! as URL) request.httpMethod = "GET" request.setValue("application/json", forHTTPHeaderField: "Content-Type") Alamofire.request(request).responseJSON(){ response in switch response.result{ case.success(let data): print("success",data) let myresponse = JSON(data) for calenadar in myresponse.array! { let calenadarObj = Calendars(calendarsJson: calenadar) self.calendarEvents.append(calenadarObj) print("notice",calenadarObj.Title) self.compareDate(date: calenadarObj.StartDate) } self.tableview.reloadData() case.failure(let error): print("Not Success",error) } } } 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 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 calendarEvents{ let date = d.StartDate 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 calendarEvents{ let date = d.StartDate 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) } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return calendarEvents.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CalendarTableViewCell let getdata = calendarEvents[indexPath.row] let date = getdata.StartDate let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" let dateFromString : NSDate = dateFormatter.date(from: date)! as NSDate dateFormatter.dateFormat = "dd-MM-yyyy" let datenew = dateFormatter.string(from: dateFromString as Date) calendar.scope = .month print("asdas",calendar.scope = .month) cell.date_txt.text = datenew cell.events_txt.text = getdata.Title calendar.reloadData() return cell } }
我已经显示了点像我有图像的事件
在图像中,我已经显示了所有JSON API事件数据数据,但它应该根据日历月份数据。 如果日历被刷到下个月,那么在tableview数据应该加载到相同的月份事件。 如何做到这一点?