Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buggy or unexpected behavior for current day text color #761

Closed
mohghazi opened this issue Apr 16, 2018 · 15 comments
Closed

Buggy or unexpected behavior for current day text color #761

mohghazi opened this issue Apr 16, 2018 · 15 comments

Comments

@mohghazi
Copy link

mohghazi commented Apr 16, 2018

Running latest version 7.1.5

Hi,

Im getting an unexpected behavior by selecting the cell of current day. CalendarView behaves normally for each selection and de-selection. Yet by selecting the current day, the view starts to behave in a strange way.

The text color should be set to red for current day. Yet other cells are getting red too! I set a breakpoint on the line where the color is set, and it is only executed when day == today. So something intern in the library is mixing things up. As red text color is observed in other cells too! Please check code sample.

Similar behavior also observed if didSelectCell- Method is used to set the red text color.

Can u plz explain this behavior?

func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
    let calendarCell = calendar.dequeueReusableJTAppleSupplementaryView(withReuseIdentifier: CustomCalendarCell.reuseIdentifier, for: indexPath) as! CustomCalendarCell
        ...
        
        calendarCell.dateLabel.text = cellState.text
    
    if cell.dateLabel.textColor == .red {
        print("red: \(date.string!)")
    }
    return calendarCell
}


func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
    
    if Calendar.current.isDateInToday(date) {
        print(date.string!)
        let calendarCell = cell as! CustomCalendarCell
        calendarCell.dateLabel.textColor = .red
    }
}
@patchthecode
Copy link
Owner

This behavior is not strange.
JTAppleCalendar is a UICollectionView.
Cells are reused.

Therefore you have to reset the cell properly.

if Calendar.current.isDateInToday(date) {
    text = red
} else {
   text = black 
}

But all you are doing is setting to red. Therefore when cells are reused, all become red.

@patchthecode
Copy link
Owner

Also, your willDisplayFunction looks strange.

it should look more like this: (pasted from my example code)

    func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
        let cell = cell as! DateCelltemplate
        cell.dateLabel.text = cellState.text
        handleCellConfiguration(cell: cell, cellState: cellState)
    }

    func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
        let cell = calendar.dequeueReusableCell(withReuseIdentifier: "DateCelltemplate", for: indexPath) as! DateCelltemplate
        self.calendar(calendar, willDisplay: cell, forItemAt: date, cellState: cellState, indexPath: indexPath)
        return cell
    }

Your cellForItem function should call the willDisplay function. And all your configuration shoul dbe done in the will display function. This has been explain in #553

@patchthecode
Copy link
Owner

closing this issue. Let me know if youre still having a problem

@mohghazi
Copy link
Author

ok. Im not using willDisplay-Method now. In cellForItem-Method I implemented this line of code to set the textColor:

cell.dateLabel.textColor = Calendar.current.isDateInToday(date) ? .red : cellTextColor
return cell

This is working fine, but not at first run i.e. when user open the calendar, even though the code is executed at first run (I set some break points and checked it). After user tap on any cell on calendar or scroll etc .. , it is set correctly.

I need to do it this way. As I have many CustomCells which are assigned accordingly to the calendar. Yet Today-Cell is an exception and need to be handled separately.

Many thanks in advance..

@patchthecode
Copy link
Owner

are you sure this is run correctly on first run?
convert this into an if statement

cell.dateLabel.textColor = Calendar.current.isDateInToday(date) ? .red : cellTextColor
// convert to this
if Calendar.current.isDateInToday(date) {
   cell.dateLabel.textColor = .red
} else {
   cell.dateLabel.textColor = cellTextColor
}

and let me know = cell.dateLabel.textColor = .red i called correctly on first run

@mohghazi
Copy link
Author

mohghazi commented Apr 18, 2018

Just tried it. Its the same behavior. For todays-Date it is executing on first run and textcolor ist set to red. Yet it is not set/visible in the calendar.

@patchthecode
Copy link
Owner

do you have a sample project with just this problem alone in it that i can take a look at?. You can create a sample project demonstrating the error. and drag the zip file in this chat box.

@mohghazi
Copy link
Author

I will try to provide sample code soon. Thanks for the support.

@patchthecode
Copy link
Owner

cool.

@mohghazi
Copy link
Author

I made a sample code. Following similar structure as in my code. Yet in sample code, it is working at first run. And I cant find the difference in implementation! I'll check again..

func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
        let cell: CustomCalendarBaseCell

        if date < Date() {
            cell = calendar.dequeueReusableCell(withReuseIdentifier: "FirstCustomCalendarCell", for: indexPath) as! FirstCustomCalendarCell
        } else {
            cell = calendar.dequeueReusableCell(withReuseIdentifier: "SecondCustomCalendarCell", for: indexPath) as! SecondCustomCalendarCell
        }
        cell.dateLabel.text = cellState.text
        if Calendar.current.isDateInToday(date) {
            cell.dateLabel.textColor = .red
        } else {
            cell.dateLabel.textColor = .green
        }
        return cell
    }

@mohghazi
Copy link
Author

mohghazi commented Apr 20, 2018

worked now :)
I have some UIAppearance stuffs in my code which i didn't use in the sample code .. so I set the textcolor using its defined property in custom cell i.e.

if Calendar.current.isDateInToday(date) {
            cell.textColor = .red
        } else {
            cell.textColor = .green
        }

Where textColor is defined in CustomCalendarBaseCell as dynamic var for dateLabel.textColor.

Many thanks and nice day

@patchthecode
Copy link
Owner

Have a good one.

@meanreaksmey
Copy link

meanreaksmey commented Aug 11, 2018

@patchthecode how can I hide cell is not not current day in month ? because if I just only hide cellview but it still have space in month.

@patchthecode
Copy link
Owner

example

@meanreaksmey
Copy link

@patchthecode thanks now it work 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants