当我开始水平滚动时,为啥我的收藏视图中的单元格被取消选择?

Posted

技术标签:

【中文标题】当我开始水平滚动时,为啥我的收藏视图中的单元格被取消选择?【英文标题】:Why my cell in my collection view getting deselected when i start scrolling horizontally?当我开始水平滚动时,为什么我的收藏视图中的单元格被取消选择? 【发布时间】:2020-05-22 11:55:30 【问题描述】:

我想从集合视图中制作一个简单的日历。 [在此处输入图片描述][1][1]:https://i.stack.imgur.com/1yXhL.jpg 但是当我选择一个单元格并开始滚动时,单元格会自动取消选择。这是我的集合视图代码。

    `let todayColor = UIColor(red: 242/255, green: 56/255, blue: 15/255, alpha: 1)
    let otherDayColor = UIColor(red: 90/255, green: 90/255, blue: 90/255, alpha: 1)
var now = Date()
    var day = DateFormatter()
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
                        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellID", for: indexPath) as! CalenderCell
                        cell.layer.cornerRadius = 5
                        cell.backgroundColor = otherDayColor
                        if indexPath.row == 0
                            cell.backgroundColor = todayColor
                            cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: Date()) - 1])
                            cell.dateLabel.text = now.string(format: "dd")
                        
                        else if indexPath.row == 1
                            cell.backgroundColor = otherDayColor
                            cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 1)) - 1])
                            cell.dateLabel.text = myCalender(numDay: 1).string(format: "dd")
                        
                        else if indexPath.row == 2
                            cell.backgroundColor = otherDayColor
                            cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 2)) - 1])
                            cell.dateLabel.text = myCalender(numDay: 2).string(format: "dd")
                        
                        else if indexPath.row == 3
                            cell.backgroundColor = otherDayColor
                            cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 3)) - 1])
                            cell.dateLabel.text = myCalender(numDay: 3).string(format: "dd")
                        
                        else if indexPath.row == 4
                            cell.backgroundColor = otherDayColor
                            cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 4)) - 1])
                            cell.dateLabel.text = myCalender(numDay: 4).string(format: "dd")
                        
                        else if indexPath.row == 5
                            cell.backgroundColor = otherDayColor
                            cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 5)) - 1])
                            cell.dateLabel.text = myCalender(numDay: 5).string(format: "dd")
            
            else if indexPath.row == 6
            cell.backgroundColor = otherDayColor
            cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 6)) - 1])
            cell.dateLabel.text = myCalender(numDay: 6).string(format: "dd")
            
            return cell
            

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
                if let cell = collectionView.cellForItem(at: indexPath)
                    if indexPath.row == 0
                        cell.backgroundColor = todayColor
                    
                    else
                        cell.backgroundColor = UIColor.systemGreen
                    
                
            

            func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) 
                if let cell = collectionView.cellForItem(at: indexPath)
                    cell.backgroundColor = otherDayColor
                
            `

【问题讨论】:

单元格在退出屏幕然后进入时取消选择? 【参考方案1】:

这是因为您在 didSelect 方法中配置了选择颜色,但在 cellForItemAt 中没有配置。您需要使用所选项目索引创建变量(并将其设置为didSelect)并在cellForItemAt 中更新颜色管理。

在视图控制器中实现选定项索引:

var selectedItem: Int?

在您的集合视图委托方法中:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellID", for: indexPath) as! CalenderCell
    cell.layer.cornerRadius = 5
    cell.backgroundColor = otherDayColor
    if indexPath.row == 0
        cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: Date()) - 1])
        cell.dateLabel.text = now.string(format: "dd")
    
    else if indexPath.row == 1
        cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 1)) - 1])
        cell.dateLabel.text = myCalender(numDay: 1).string(format: "dd")
    
    else if indexPath.row == 2
        cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 2)) - 1])
        cell.dateLabel.text = myCalender(numDay: 2).string(format: "dd")
    
    else if indexPath.row == 3
        cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 3)) - 1])
        cell.dateLabel.text = myCalender(numDay: 3).string(format: "dd")
    
    else if indexPath.row == 4
        cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 4)) - 1])
        cell.dateLabel.text = myCalender(numDay: 4).string(format: "dd")
    
    else if indexPath.row == 5
        cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 5)) - 1])
        cell.dateLabel.text = myCalender(numDay: 5).string(format: "dd")
    
    else if indexPath.row == 6
        cell.dayLabel.text = (day.shortWeekdaySymbols[Calendar.current.component(.weekday, from: myCalender(numDay: 6)) - 1])
        cell.dateLabel.text = myCalender(numDay: 6).string(format: "dd")
    

    if indexPath.row == 0 
        cell.backgroundColor = todayColor
     else 
        if let selectedItem = selectedItem,
           selectedItem == indexPath.row 
            // this cell is selected
            cell.backgroundColor = UIColor.systemGreen
         else 
            cell.backgroundColor = otherDayColor
        
    
    return cell


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
    if let cell = collectionView.cellForItem(at: indexPath) 
        selectedItem = indexPath.row // set selectedItem
        if indexPath.row == 0 
            cell.backgroundColor = todayColor
         else
            cell.backgroundColor = UIColor.systemGreen
        
    

didDeselectItemAt中需要设置selectedItem为nil。

使用switch-case 代替if 也很好

【讨论】:

没有。问题是一样的。当我滚动所有其他单元格时,只有最后一个单元格被取消选中。 你在 'didSelectItemAt' 中设置了 selectedItem = indexPath.row 吗? 是的,我已将它添加到我的 didSelectItemAtIndex 函数中。 在底部的“cellForItemAt”中添加调试:cell.dateLabel.textColor = cell.backgroundColor

以上是关于当我开始水平滚动时,为啥我的收藏视图中的单元格被取消选择?的主要内容,如果未能解决你的问题,请参考以下文章

当我将滚动方向从垂直更改为水平时,为啥集合视图会崩溃?

为啥当我滚动我的 tabelview 单元格时我的 imageview 正在缩小?

为啥滚动后我的单元格中的图标会发生变化? [复制]

为啥我的收藏视图在向下或向上滚动时很慢?如何解决性能问题?我已经清理了我的 UI 代码

为啥偶尔会在 ListView 中加载错误的图像?

UITableViewCell 中的 UIScrollview 和维护滚动视图页面