Andre
Andre

Reputation: 7668

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.

Upvotes: 0

Views: 917

Answers (3)

Just a coder
Just a coder

Reputation: 16730

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.

Upvotes: 1

Ladislav
Ladislav

Reputation: 7283

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
}

Upvotes: 0

Tarek A.
Tarek A.

Reputation: 238

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())!
}

Upvotes: 0

Related Questions