collectionView 单元格在 reloadData() 之后随机突出显示

collectionView cells randomly gets highlighted after reloadData()

我正在练习使用 collectionView 构建日历。

我的实现现在很简单,主要是 https://github.com/Akhilendra/calenderAppiOS/tree/master/myCalender2, just image a simple calendar.

的副本

我正在尝试通过让用户 select 他们想要的日期并在 selected 单元格后创建一个小边框来对其进行一些改进。

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
}

现在的问题是 selected 单元格会在我的 collectionView 周围跳来跳去,因为重用功能。我做了一些研究,解决这个问题的方法是避免使用 reloadData,但是当我转发到下个月时,我需要刷新我的整个 collectionView。

有人可以帮我解决这个问题吗?我想在用户移动到下个月后清除 selected 单元格,并在用户执行 selections 的月份维护 selected 单元格。

提前致谢!

问题:

单元格在您的 collectionView 中随机突出显示的原因是因为单元格在 CollectionView 和 TableView 中被重复使用。当用户选择单元格时,您会通过更改其边框宽度和边框颜色来突出显示该单元格,但是当用户滚动时,同一单元格会被其他一些 indexPath 重用,并且因为当单元格被重用时您没有删除应用的效果,单元格会在错误的位置突出显示。

解法:

因为您有自己的自定义单元格 class,所以您可以随时使用 prepareForReuse 清除单元格重复使用时应用于单元格的效果。

因此打开您的 CustomCell class 并添加

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
}

在上面的代码中,else 条件不是必需的,因为我们已经准备好了 prepareForReuse,它会删除所有添加到单元格的高亮显示。

希望对您有所帮助:)