박현호
박현호

Reputation: 51

didSelectItemAt and didDeSelectItemAt from UICollectionView

If I use didSelectItemAt in a UICollectionView, I want to change the color of the selected cell. And with didDeselectItemAt, I want to replace the color of the previously selected cell.

It works well before moving. However, moving to the far right will not work properly. Why is this?

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return 14 
 }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalendarCollectionViewCell", for: indexPath ) as! CalendarCollectionViewCell

        cell.calendarDayLabel.text = calendar?.twoWeeksDay![ indexPath.row ]
        cell.calendarDateLabel.text = calendar?.twoWeeksDate![ indexPath.row ]

        return cell

 }

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let cell = collectionView.cellForItem(at: indexPath) as? CalendarCollectionViewCell


        cell?.calendarDayLabel.textColor = UIColor( red: 255, green: 0, blue: 0, alpha: 1.0 )
        cell?.calendarDateLabel.textColor = UIColor( red: 255 , green: 255 , blue: 255 , alpha: 1.0 )
        cell?.calendarCircleImageView.isHidden = false

        self.selectYear = self.calendar?.twoWeeksYear![ indexPath.row ]
        self.selectMonth = self.calendar?.twoWeeksMonth![ indexPath.row ]
        self.selectDate = self.calendar?.twoWeeksDate![ indexPath.row ]
        self.selectDay = self.calendar?.twoWeeksDay![ indexPath.row ]

        self.selectDateTime.text = self.selectYear! + "년 " + self.selectMonth! + "월 " + self.selectDate! + "일 " + self.selectDay!

        self.selectDateTime.isHidden = false

}

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {


        let cell = collectionView.cellForItem(at: indexPath) as? CalendarCollectionViewCell
        cell?.calendarDayLabel.textColor = UIColor( red: 0, green: 0, blue: 0, alpha: 1.0 )
        cell?.calendarDateLabel.textColor = UIColor( red: 0 , green: 0 , blue: 0 , alpha: 1.0 )
        cell?.calendarCircleImageView.isHidden = true

 }

enter image description here

Upvotes: 5

Views: 7126

Answers (2)

Procrastin8
Procrastin8

Reputation: 4503

What are you trying to achieve here? Multi-selection state? Or just a selection state? Either way, you really ought to be subclassing UICollectionViewCell and overriding isSelected instead of having the collection view delegate manage the look of selection.

For the first example, just enable allowsMultipleSelection on the collection view and select cells as they are tapped. You can override isHighlighted to give a temporary look to the cell a user is touching.

For the latter, you only need to override isSelected and set a color based on this state.

override var isSelected: Bool {
    didSet {
        if isSelected {
            calendarDayLabel.textColor = UIColor( red: 255, green: 0, blue: 0, alpha: 1.0 )
            calendarDateLabel.textColor = UIColor( red: 255 , green: 255 , blue: 255 , alpha: 1.0 )
        } else {
            // do opposite color
        }
        calendarCircleImageView.isHidden = !isSelected
     }
}

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100533

Because cells are reused

 var selectedIndex:IndexPath?

//

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CalendarCollectionViewCell", for: indexPath ) as! CalendarCollectionViewCell

       if indexPath == selectedIndex {

                cell.calendarDayLabel.textColor = UIColor( red: 255, green: 0, blue: 0, alpha: 1.0 )
                cell.calendarDateLabel.textColor = UIColor( red: 255 , green: 255 , blue: 255 , alpha: 1.0 )
                cell.calendarCircleImageView.isHidden = false

       }
       else {

              cell.calendarDayLabel.textColor = UIColor( red: 0, green: 0, blue: 0, alpha: 1.0 )
              cell.calendarDateLabel.textColor = UIColor( red: 0 , green: 0 , blue: 0 , alpha: 1.0 )
              cell.calendarCircleImageView.isHidden = true

       }


}

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    selectedIndex = indexPath
    collectionView.reloadData()

 }

Remove didDeselectItemAt

Upvotes: 4

Related Questions