简体   繁体   中英

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. It's an extension of a collectionView .

You can use 2 techniques to break the loop.

First :

calendarViewselectDates(from: starOfTheWeek, to: endOfTheWeeK, triggerSelectionDelegate: false)

By setting triggerSelectionDelegate to false, your delegate function didSelect will not be called.


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 . You know this by --> cellState.

if cellState.selectionType == .programatic {
   // ignore stuff        
} else {
   // Do stuff
}

You can put this if statement in your shouldSelect function.

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM