patchthecode / JTAppleCalendar

The Unofficial Apple iOS Swift Calendar View. Swift calendar Library. iOS calendar Control. 100% Customizable
https://patchthecode.com
MIT License
7.57k stars 810 forks source link

Problems changing the background of cell at method cellForItemAt #657

Closed CassianoMouraLeao closed 6 years ago

CassianoMouraLeao commented 6 years ago
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {

        let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CustomCell", for: indexPath) as! Calendar_CollectionViewCell

        let year = Calendar.current.component(.year, from: date)
        let month = Calendar.current.component(.month, from: date)
        let day = Calendar.current.component(.day, from: date)

        for i in 0...myArray {

            let yearFor = Calendar.current.component(.year, from: array[i])
            let monthFor = Calendar.current.component(.month, from: array[i])
            let dayFor = Calendar.current.component(.day, from: array[i])

            if year == yearFor && month == monthFor && day == dayFor {
                cell.myImage.isHidden = false
            } else {
                cell.myImage.isHidden = true
            }
        }

        return cell
    }

In this code I can see just cells with cell.myImage.isHidden = true but when I debug my code pass at line cell.myImage.isHidden = false twice but I cannot see the image in application.

Can anyone help me? I think that is the same idea of "Cells are reused." but I don't know how I can resolve.

patchthecode commented 6 years ago

hmm. again, you are using UICOllectionViews/UITableviews. There is no need to loop through your datasource to set the cells. The function

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

will be called for every cell.

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

        let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CustomCell", for: indexPath) as! Calendar_CollectionViewCell

        let year = Calendar.current.component(.year, from: date)
        let month = Calendar.current.component(.month, from: date)
        let day = Calendar.current.component(.day, from: date)

            let yearFor = Calendar.current.component(.year, from: array[indexpath.item])
            let monthFor = Calendar.current.component(.month, from: array[indexpath.item])
            let dayFor = Calendar.current.component(.day, from: array[indexpath.item])

            if year == yearFor && month == monthFor && day == dayFor {
                cell.myImage.isHidden = false
            } else {
                cell.myImage.isHidden = true
            }

        return cell
    }

let me know if this works

CassianoMouraLeao commented 6 years ago

It doesn't work yet, because my array has 16 values and the indexPath work between 0 to 16 that is 17 and now I have this error: Fatal error: Index out of range. Can I change the value of indexPath? I need to change the 0 to 16 for 1 to 16.

patchthecode commented 6 years ago

ok. i just gave an example. How it should be is like how i had in the example video. The best structure to store events in is a Dictionary of the form [String : YourCustomEventObject]

If you need more information you can chat here -> https://gitter.im/patchthecode/JTAppleCalendar As it is faster than typing in the chat box.