繁体   English   中英

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

[英]collectionView cells randomly gets highlighted after reloadData()

我正在练习使用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,所以不需要其他条件,它可以删除所有添加到单元格中的突出显示。

希望能帮助到你 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM