- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如果我在 UICollectionView
中使用 didSelectItemAt
,我想更改所选单元格的颜色。使用 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 - 来自 UICollectionView 的 didSelectItemAt 和 didDeSelectItemAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526703/
我正在尝试实现此功能:在我的应用程序中,如果我在 UICollectionView 中选择了一个单元格,则边框会变为蓝色,如果我选择另一个单元格,则应取消选择前一个单元格并且边框应变为透明。我写了一些
I have a UICollectionView, and when selecting an item didDeselectItemAt not get triggered, but when
我正在显示一个 Collection View ,以便用户可以选择类别,然后我将填充 TableView。 问题是,当集合单元格不在 View 中时,didDeselectItemAt 函数没有响应。
如果我在 UICollectionView 中使用 didSelectItemAt,我想更改所选单元格的颜色。使用 didDeselectItemAt,我想替换之前选择的单元格的颜色。 搬家前效果很好
我正在使用 didSelectItemAt 和 didDeselectItemAt 进行 collectionViewCell 的多重选择。我想选择单元格并在选中时将边框设为蓝色,同时取消选择“选定”
这个问题在这里已经有了答案: How can I fix crash when tap to select row after scrolling the tableview? (2 个答案) 关闭
我是一名优秀的程序员,十分优秀!