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

时间:2017-12-31 03:45:19

标签: ios swift4 xcode9

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

我的实现现在很简单,主要是https://github.com/Akhilendra/calenderAppiOS/tree/master/myCalender2的副本,只是简单的日历。 enter image description here

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

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。

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

提前谢谢!

1 个答案:

答案 0 :(得分:2)

<强>问题:

单元格在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,它可以删除添加到单元格中的所有高光。

希望有所帮助:)