iOS swift: select 一个单元格我想 select 整行
iOS swift: selecting a cell I want to select the entire row
我有一个日历,当我每天 select 时,我想 select 整行(一周).. 这是我目前的代码:
//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())!
}
}
问题是我有一个无限循环,原因很清楚。
但我不知道如何防止它。有帮助吗?
JTAppleCalender
是一个外部库。它是 collectionView
.
的扩展
我知道这不是最好的解决方案,但它可以避免您的问题
//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())!
}
只需添加一个新的布尔变量,例如 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
}
您可以使用 2 种技巧来打破循环。
第一个:
calendarViewselectDates(from: starOfTheWeek, to: endOfTheWeeK, triggerSelectionDelegate: false)
通过将 triggerSelectionDelegate
设置为 false,您的委托函数 didSelect
将不会被调用。
第二个:
如果您正在使用 MasterBranch
代码(我将在一周左右发布),您可以知道您的选择是否是 程序员发起的 vs 用户启动。你知道这个 --> cellState.
if cellState.selectionType == .programatic {
// ignore stuff
} else {
// Do stuff
}
您可以将此 if 语句放入 shouldSelect
函数中。
我有一个日历,当我每天 select 时,我想 select 整行(一周).. 这是我目前的代码:
//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())!
}
}
问题是我有一个无限循环,原因很清楚。 但我不知道如何防止它。有帮助吗?
JTAppleCalender
是一个外部库。它是 collectionView
.
我知道这不是最好的解决方案,但它可以避免您的问题
//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())!
}
只需添加一个新的布尔变量,例如 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
}
您可以使用 2 种技巧来打破循环。
第一个:
calendarViewselectDates(from: starOfTheWeek, to: endOfTheWeeK, triggerSelectionDelegate: false)
通过将 triggerSelectionDelegate
设置为 false,您的委托函数 didSelect
将不会被调用。
第二个:
如果您正在使用 MasterBranch
代码(我将在一周左右发布),您可以知道您的选择是否是 程序员发起的 vs 用户启动。你知道这个 --> cellState.
if cellState.selectionType == .programatic {
// ignore stuff
} else {
// Do stuff
}
您可以将此 if 语句放入 shouldSelect
函数中。