[英]iOS swift: selecting a cell I want to select the entire row
I have a calendar, and I want to select an entire row (week) when I select a day.. this is my code so far: 我有一个日历,当我选择一天时,我想选择整行(一周)。这是到目前为止的代码:
//When a date is selected
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
selectTheWeek(of: date)
setupLayoutCell(cell: cell, cellState: cellState)
}
func selectTheWeek(of date: Date) {
let starOfTheWeek = date.startOfWeek()
let endOfTheWeeK = date.endOfWeek()
calendarCollectionView.selectDates(from: starOfTheWeek, to: endOfTheWeeK)
}
extension Date {
func startOfWeek() -> Date {
let calendar = Calendar.autoupdatingCurrent
let currentDateComponents = calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)
return calendar.date(from: currentDateComponents)!
}
func endOfWeek() -> Date {
let cal = Calendar.autoupdatingCurrent
var component = DateComponents()
component.weekOfYear = 1
component.day = -1
return cal.date(byAdding: component, to: startOfWeek())!
}
}
the problem is that I'm having an infinite loop, and it's clear the reason. 问题是我遇到了无限循环,原因很明显。 But I don't know how to prevent it. 但是我不知道如何预防。 Any help? 有什么帮助吗?
JTAppleCalender
is an external library. JTAppleCalender
是一个外部库。 It's an extension of a collectionView
. 它是collectionView
的扩展。
You can use 2 techniques to break the loop. 您可以使用2种技巧来打破循环。
First : 首先 :
calendarViewselectDates(from: starOfTheWeek, to: endOfTheWeeK, triggerSelectionDelegate: false)
By setting triggerSelectionDelegate
to false, your delegate function didSelect
will not be called. 通过将triggerSelectionDelegate
设置为false,将不会调用您的委托函数didSelect
。
Second: 第二:
If you are using MasterBranch
code (which i'll be releasing in a week or so), you can know whether or not your selection is programmer initiated vs user initiated . 如果您使用的是MasterBranch
代码(我将在一周左右的时间内发布该代码),则可以知道您的选择是否是程序员发起的还是 用户发起的 。 You know this by --> cellState. 您知道-> cellState。
if cellState.selectionType == .programatic {
// ignore stuff
} else {
// Do stuff
}
You can put this if statement in your shouldSelect
function. 您可以将此if语句放入shouldSelect
函数中。
I know this is a not the best possible solution but it could work to avoid your issue 我知道这不是最好的解决方案,但可以避免出现问题
//When a date is selected
var shouldSelectWeek = true
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
if shouldSelectWeek{
selectTheWeek(of: date)
shouldSelectWeek = false
}
setupLayoutCell(cell: cell, cellState: cellState)
}
func selectTheWeek(of date: Date) {
let starOfTheWeek = date.startOfWeek()
let endOfTheWeeK = date.endOfWeek()
calendarCollectionView.selectDates(from: starOfTheWeek, to: endOfTheWeeK)
}
extension Date {
func startOfWeek() -> Date {
let calendar = Calendar.autoupdatingCurrent
let currentDateComponents = calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)
return calendar.date(from: currentDateComponents)!
}
func endOfWeek() -> Date {
let cal = Calendar.autoupdatingCurrent
var component = DateComponents()
component.weekOfYear = 1
component.day = -1
return cal.date(byAdding: component, to: startOfWeek())!
}
Just add a new boolean variable like var shouldIgnoreDateSelection = false
and then you just do 只需添加一个新的布尔变量,如var shouldIgnoreDateSelection = false
,然后就可以了
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
if shouldIgnoreDateSelection == false {
selectTheWeek(of: date)
}
setupLayoutCell(cell: cell, cellState: cellState)
}
func selectTheWeek(of date: Date) {
let starOfTheWeek = date.startOfWeek()
let endOfTheWeeK = date.endOfWeek()
shouldIgnoreDateSelection = true
calendarCollectionView.selectDates(from: starOfTheWeek, to: endOfTheWeeK)
shouldIgnoreDateSelection = false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.