我正在练习使用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 中被重用。当用户选择单元格时,您通过更改其边框宽度和边框颜色来突出显示单元格,但是当用户滚动时,同一单元格会被其他索引路径重用,并且因为当单元格被重用时您没有删除应用的效果,因此单元格会在错误的位置突出显示。
解决方案:
因为您有自己的自定义单元格类,所以您始终可以使用 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,并且它可以删除添加到单元格中的所有突出显示。
希望有帮助:)
关于ios - 在 reloadData() 之后,collectionView 单元格会随机突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48039800/