我正在练习使用collectionView来构建日历。
我的实现现在很简单,主要是https://github.com/Akhilendra/calenderAppiOS/tree/master/myCalender2的副本,只是图像简单的日历。
我试图通过让用户选择他们想要的日期并在选择单元格后创建一个小边框来对其进行一些改进。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCell
cell.backgroundColor=UIColor.clear
if indexPath.item <= firstWeekDayOfMonth - 2 {
print(firstWeekDayOfMonth)
cell.isHidden=true
} else {
let calcDate = indexPath.row-firstWeekDayOfMonth+2
cell.isHidden=false
cell.dateLabel.text="\(calcDate)"
if calcDate < todaysDate && currentYear == presentYear && currentMonthIndex == presentMonthIndex {
cell.isUserInteractionEnabled=true
cell.dateLabel.textColor = UIColor.darkGray
} else {
cell.isUserInteractionEnabled=true
cell.dateLabel.textColor = UIColor.black
}
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
addToList.append(indexPath)
//dayCollectionView.deselectItem(at: indexPath, animated: true)
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderWidth = 2.0
cell?.layer.borderColor = UIColor.gray.cgColor
}
现在的问题是所选单元格将跳转到我的collectionView周围,因为重用功能。我已经做了一些研究,解决这个问题的方法是避免使用reloadData,但是当我转发到下个月时,我需要刷新我的整个collectionView。
有人可以帮我解决这个问题吗?我想在用户移动到下个月后清除所选单元格,并在用户进行选择的月份维护所选单元格。
先感谢您!
问题:
在您的collectionView中随机突出显示单元格的原因是因为单元格在CollectionView和TableView中被重用。当用户选择单元格时,通过更改其边框宽度和边框颜色来突出显示单元格,但是当用户滚动时,相同的单元格将被重用于其他一些indexPath,并且因为在单元格重用时未删除应用的效果,单元格会在错误的位置突出显示。
方案:
因为您有自己的自定义单元类,所以在单元格重用时,您始终可以使用prepareForReuse
清除应用于单元格的效果。
所以打开你的CustomCell类并添加
override func prepareForReuse() {
super.prepareForReuse()
self.layer.borderWidth = 1.0 //whatever the default border width is
self.layer.borderColor = UIColor.clear.cgColor //whatever the cell's default color is
}
但这只解决了你问题的一半,现在虽然当用户滚动回选定的单元格时,不需要的单元格不会突出显示,即使选定的单元格也不会再突出显示。为了实现这一点,如果需要在cellForRowAtIndexPath
中突出显示,则需要有条件地突出显示单元格
所以修改你的cellForRowAtIndexPath
为
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCell
cell.backgroundColor=UIColor.clear
if indexPath.item <= firstWeekDayOfMonth - 2 {
print(firstWeekDayOfMonth)
cell.isHidden=true
} else {
let calcDate = indexPath.row-firstWeekDayOfMonth+2
cell.isHidden=false
cell.dateLabel.text="\(calcDate)"
if calcDate < todaysDate && currentYear == presentYear && currentMonthIndex == presentMonthIndex {
cell.isUserInteractionEnabled=true
cell.dateLabel.textColor = UIColor.darkGray
} else {
cell.isUserInteractionEnabled=true
cell.dateLabel.textColor = UIColor.black
}
//check if cell needs to be highlighted
//else condition isnt required because we have prepareForReuse in place
if addToList.contains(indexPath) {
cell?.layer.borderWidth = 2.0
cell?.layer.borderColor = UIColor.gray.cgColor
}
}
return cell
}
在上面的代码中,不需要条件,因为我们已准备好prepareForReuse,它可以删除添加到单元格的所有高亮显示。
希望能帮助到你 :)