问题描述
我正在使用 FSCalendar,我想根据我的要求对其进行自定义。所以,我在视图控制器中有一个月份列表,并且同一个控制器有 FSCalendar。我想要的是,当我点击任何特定月份时,日历只会更改为该月份。
月份列表在集合视图中。单击单元格时,我显示的是上个月,但我不知道如何显示任何特定月份。
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
if let currentCell = collectionView.cellForItem(at: indexPath) as? AttendenceMonthCell {
// what code should I write here to get the particular month
let month = Calendar.current.date(byAdding: .month,value: -1,to: calender.currentPage)
calender.setCurrentPage(month!,animated: true)
}
}
@H_502_6@
我在谷歌上搜索了很多,但还没有得到任何答案。这可以通过 FSCalendar 实现吗?
解决方法
首先,找到当前月份并从选定的单元格索引中减去。像这样。
let index = indexPath.item + 1
let currentMonth = Calendar.current.component(.month,from: Date())
let month = Calendar.current.date(byAdding: .month,value: index - currentMonth,to: calender.currentPage)
calender.setCurrentPage(month!,animated: true)