ios - 来自 UICollectionView 的 didSelectItemAt 和 didDeSelectItemAt
问题描述
如果我didSelectItemAt
在 a 中使用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
}
解决方案
因为细胞被重复使用
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
推荐阅读
- ios - 当想要使用 UISlider 增加视图大小时,CGAffineTransformRotate 设置为其初始状态
- c - 这个程序的输出是什么?为什么?
- reactjs - 如何在 reactjs 中为子组件触发事件处理程序
- android - 自定义可绘制对象在应用于布局时无法正确显示
- unit-testing - 如何在go中测试db错误?
- facebook - 是否可以在 Graph-API 中使用应用令牌获取 facebook 页面数据?
- python - 在 MySQL 中查询字符串时“'utf-8'编解码器无法解码字节”
- javascript - 下拉搜索功能超过 30 万条记录
- google-sheets - 使用动态值查询和 ImportRange
- docker - Bundler::GemNotFound:在任何源中都找不到 rake-12.3.2