Search code examples
iosswiftuicollectionviewuicollectionviewcellfscalendar

Accessing FSCalendar function inside custom cell


I have a FSCalendar view within a UICollectionViewCell which is connected and functional. However, when calling the didSelect date function within an extension of the viewController nothing is actioned (the print function is not called). Is a specific reference required to be passed to CellForIndexPath to call this function?

Extension Code:

extension CalendarViewController: FSCalendarDelegate, FSCalendarDataSource {

    func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {

    print("Date selected")
    }
}

Custom Cell:

class ChartCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var calendarView: FSCalendar!

}

CollectionView cellForItemAt code:

   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


        if indexPath.item == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "chartCell", for: indexPath) as! ChartCollectionViewCell

            return cell
        }
   }

Solution

  • Add this line inside cellForItemAt

    cell.calendarView.delegate = self