我在日历中显示了事件点但是根据tableview中的月份无法显示相同的事件,我想显示那些事件,如果我们更改月份和tableview数据事件应该更改,我很困惑如何可以办到 。我正在使用FSCalendar来显示dot和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数据中应加载到相同月份的事件。怎么做?