进入编辑模式时,CollectionView 单元格不一致地显示复选框

问题描述 投票:0回答:1

进入编辑模式时,我的 UICollectionView 遇到问题。问题是,当我向下滚动时,某些单元格会显示复选框,而其他单元格则不会。但是,我希望所有单元格在编辑模式下始终显示复选框。

其他背景:

  • 我正在为我的 UICollectionView 使用自定义布局。
  • 我已经实现了自定义单元格(在本例中为 MailListCell)。
  • 当我在编辑模式下向下滚动集合视图时,会出现此问题。

预期行为: 集合视图中的所有单元格在编辑模式下都应一致显示复选框,无论它们是否可见。

override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        
        if editing {
            // Enable multi-selection, show checkboxes, etc.
            navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelEditing))
            
            //shift the list to right
//            collectionViewLeadingConstraint?.constant = 50
            
            // Show the checkboxes and move labels to right
                    for indexPath in mailListCollectionView.indexPathsForVisibleItems {
                        if let cell = mailListCollectionView.cellForItem(at: indexPath) as? MailListCell {
                            cell.moveLabelsToRight()
//                            cell.roundCheckbox.isHidden = false
//                            cell.animateCheckboxAppearance()
                        }
                    }
            mailListCollectionView.allowsMultipleSelection = true
        } else {
            // Disable multi-selection, hide checkboxes, etc.
            navigationItem.rightBarButtonItem = editButtonItem
            
            //bring the list back to original position
//            collectionViewLeadingConstraint?.constant = 0
            
                    for indexPath in mailListCollectionView.indexPathsForVisibleItems {
                        if let cell = mailListCollectionView.cellForItem(at: indexPath) as? MailListCell {
                            cell.moveLabelsToOriginalPosition()
//                            cell.roundCheckbox.isHidden = true
                        }
                    }
        }
        
        // need to call layoutIfNeeded to apply the constraint changes
            UIView.animate(withDuration: 0.3) {
                self.view.layoutIfNeeded()
            }
    }
    @objc func cancelEditing() {
        // Implement any logic needed when the user cancels the editing mode.
        // For example, deselect any selected cells, hide checkboxes, etc.
        
        setEditing(false, animated: true)
        mailListCollectionView.allowsSelection = false
    }

以下是进入和退出编辑模式时使屏幕发生变化的功能。

extension MailListCell {
    func moveLabelsToRight() {
        senderLeadingConstraint?.constant = 50
        subjectLeadingConstraint?.constant = 50
        dateLeadingConstraint?.constant = 50
        separatorLeadingConstraint?.constant = 50
        roundCheckboxLeadingConstraint?.constant = 10
        
        // Change alpha value to create a fading effect
        UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseInOut, animations: {
                    self.roundCheckbox.alpha = 1
                }, completion: nil)
    }

    func moveLabelsToOriginalPosition() {
        senderLeadingConstraint?.constant = 16
        subjectLeadingConstraint?.constant = 16
        dateLeadingConstraint?.constant = 16
        separatorLeadingConstraint?.constant = 16
        roundCheckboxLeadingConstraint?.constant = -20
        
        // Change alpha value to create a fading effect
        UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseInOut, animations: {
                    self.roundCheckbox.alpha = 0
                }, completion: nil)
    }
}

任何有关如何确保编辑模式下复选框可见性一致的见解或建议将不胜感激。

ios swift uicollectionview uikit
1个回答
0
投票

您应该按照 @Paulw11 在他的评论中所说的方式处理

cellForItem
数据源。我假设你有一个启用编辑模式的功能,例如:

func startEditing() {
    ...
    isEditingMode = true
    mailListCollectionView.allowsMultipleSelection = true
    mailListCollectionView.reloadData()
}

然后在

cellForItem
函数中:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = ... as! MailListCell
    if isEditingMode {
        cell.moveLabelsToRight()
    } else {
        cell.moveLabelsToOriginalPosition()
    }
    return cell
}
© www.soinside.com 2019 - 2024. All rights reserved.