如果我在didSelectItemAt
中使用UICollectionView
,我想更改所选单元格的颜色。并使用didDeselectItemAt
,我要替换先前选择的单元格的颜色。
在移动之前效果很好。但是,移到最右边将无法正常工作。为什么会这样?
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
}
答案 0 :(得分:2)
因为细胞被重复使用
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()
}
删除didDeselectItemAt
答案 1 :(得分:0)
您要在这里实现什么?多选状态?还是只是选择状态?无论哪种方式,您都应该真正地继承UICollectionViewCell
并覆盖isSelected
,而不是让集合视图委托来管理选择的外观。
对于第一个示例,只需在集合视图上启用allowsMultipleSelection
并在点击它们时选择单元格即可。您可以覆盖isHighlighted
来临时查看用户正在触摸的单元格。
对于后者,您只需要覆盖isSelected
并根据此状态设置颜色即可。
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
}
}