Search code examples
iosswiftuitableviewswift4.1fscalendar

How to show the events data in FSCalander according to particular date and days range?


Hi I have following response

  json: {
  "status" : "success",
  "data" : {
    "classes" : {
      "SUNDAY" : [
        {
          "latitude" : 32.3844785674545,
          "uploads" : "https:\/\/xxxxx\/Uploads\/Class\/swimming.png",
          "location" : "dubai",
          "id" : 3,
          "startDate" : "04-08-2018",
          "endDate" : "05-08-2018",
          "description" : "Basic Swimming Tactics for women trained by women.",
          "title" : "Swimming Class",
          "endTime" : "05:20",
          "longitude" : 23.465767967087778,
          "startTime" : "03:10",
          "subTitle" : "Basic Swimming Tactics"
        }
      ],
      "WEDNESDAY" : [

      ],
      "THURSDAY" : [

      ],

I already fetched the data in tableview by manually calling a particular day. enter image description here

as below .

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     //   return tableViewImageArray.count
        return classes.count
    }

    // Table View Delegates
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let tCell = tableView.dequeueReusableCell(withIdentifier: "schedulesCell", for: indexPath) as! schedulesTVCell
       // tCell.classThumb.image = tableViewImageArray[indexPath.row]
       // tCell.classTitle.text = classNameArray[indexPath.row]
        let allClasses = classes[indexPath.row]
        tCell.classTitle.text = allClasses.title
        tCell.classDesc.text = allClasses.description
        tCell.classLocation.text = allClasses.location

        let cImgUrl = allClasses.uploadURL

        //make the thumb image round
        tCell.classThumb.layer.borderWidth = 1
        tCell.classThumb.layer.masksToBounds = false
        tCell.classThumb.layer.borderColor = UIColor.lightGray.cgColor
        tCell.classThumb.layer.cornerRadius = tCell.classThumb.frame.height/2
        tCell.classThumb.clipsToBounds = true


        Alamofire.request(cImgUrl).responseData(completionHandler: { response in
            if let image1 = response.result.value {
                self.thumbImage = UIImage(data: image1)!
                tCell.classThumb.image = self.thumbImage
                print("IMG", self.thumbImage! )

            }
        })

        return tCell;
    }

I just want to load tableview according to the days I clicked between a particular date range (startDate and EndDate). in the following tableview below.

Please help me by providing a clear example (complete code is appreated) according to my response since I never implemented a FSCalander.

Thanks in Advance.


Solution

  • step 1)

     @IBOutlet var calender: FSCalendar!
    

    step 2)

        calender.select(Date())
        calender.scope = .week
        calender.accessibilityIdentifier = "calender"
    

    Step 3)

      callApiWillGiveresultbasedonselectedDate(currentselectedDate:String)
    

    Based on array response of api display in tableview reload on every api call

    Step 4)

    //MARK: - Calender Method
        func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
            self.calendarHeightConstraint.constant = bounds.height
            self.view.layoutIfNeeded()
        }
    
        func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
            let selectedDates = calendar.selectedDates.map({self.dateFormatter.string(from: $0)})
            print("selected dates is \(selectedDates)")
            currentselected = "\(self.dateFormatter.string(from: date))"
            callApiWillGiveresultbasedonselectedDate(currentselectedDate: currentselected)
            if monthPosition == .next || monthPosition == .previous {
                calendar.setCurrentPage(date, animated: true)
            }
        }
    
        func calendarCurrentPageDidChange(_ calendar: FSCalendar) {
            print("\(self.dateFormatter.string(from: calendar.currentPage))")
        }