在进入编辑模式时,我的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)
}
}
型
任何关于如何在编辑模式下确保一致的复选框可见性的见解或建议将不胜感激。
更新代码:
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
if editing {
isEditingMode = true
// 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 {
isEditingMode = false
// 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()
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! MailListCell
if isEditingMode {
cell.moveLabelsToRight()
} else {
cell.moveLabelsToOriginalPosition()
}
cell.senderLabel.text = "some sender"
cell.subjectLabel.text = "some subject"
cell.dateLabel.text = "dd/mm/yyyy"
return cell
}
型
1条答案
按热度按时间hmmo2u0o1#
你应该像@Paulw11在他的评论中说的那样处理
cellForItem
的问题。我假设你会有一个功能来启用编辑模式,比如:字符串
然后在
cellForItem
函数中:型