I have displayed events dots in calendar but not able to display same events according to month in tableview , I want to display those events like if we change the month and tableview data events should be changed , I'am confused how it can be done . I'am using FSCalendar to display events like dot and in 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
}
}
i have displayed events in dots like i have image
In image i have displayed all JSON api events datas data but it should be according to calendar month data. if calendar is swiped to next month then in tableview data should load to same months events. How it can be done?