用 属性 观察者更改集合视图单元格不好吗?
Is it bad to change collection view cell with property observer?
class SceneCell: UICollectionViewCell {
override var isSelected: Bool {
didSet {
setSelected(bool: isSelected)
}
}
override var isHighlighted: Bool {
didSet {
setHighlighted(bool: isHighlighted)
}
}
@IBOutlet weak var thumbnailImageView: UIImageView!
override func draw(_ rect: CGRect) {
super.draw(rect)
self.backgroundColor = .clear
self.thumbnailImageView.layer.borderColor = UIColor.green.cgColor
self.thumbnailImageView.layer.masksToBounds = true
self.thumbnailImageView.clipsToBounds = true
self.thumbnailImageView.layer.cornerRadius = 8
}
func update(with scene: Scene) {
}
private func setHighlighted(bool: Bool) {
if bool {
self.alpha = 0.5
} else {
self.alpha = 1.0
}
}
private func setSelected(bool: Bool) {
if bool {
self.thumbnailImageView.layer.borderWidth = 2.5
} else {
self.thumbnailImageView.layer.borderWidth = 0
}
}
}
在我的代码中,当 isSelected 设置为真时,我将图像视图的图层边框宽度更改为 2.5。
当我 select 一个单元格并滚动集合视图时,我认为单元格在重用 selected 单元格时保持 selected 状态,但重用单元格变为 unselected状态。其次,当我回到 selected 单元格并重新使用 unselected 单元格时,我认为它处于 unselected 状态。但是单元格自动设置 selected.
集合视图是否自动管理这些?
问题的代码运行良好。这是记录单元格选择并应用 selected/deselected 状态设置的替代解决方案。
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
//......
var selectedIndexPaths = [IndexPath]()
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndexPaths.append(indexPath)
}
public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let index = selectedIndexPaths.index(of: indexPath) {
selectedIndexPaths.remove(at: index)
}
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//...
cell.setSelected(selectedIndexPaths.contains(indexPath)) //Remember to make the cell's setSelected() public.
//...
}
//......
}
class SceneCell: UICollectionViewCell {
override var isSelected: Bool {
didSet {
setSelected(bool: isSelected)
}
}
override var isHighlighted: Bool {
didSet {
setHighlighted(bool: isHighlighted)
}
}
@IBOutlet weak var thumbnailImageView: UIImageView!
override func draw(_ rect: CGRect) {
super.draw(rect)
self.backgroundColor = .clear
self.thumbnailImageView.layer.borderColor = UIColor.green.cgColor
self.thumbnailImageView.layer.masksToBounds = true
self.thumbnailImageView.clipsToBounds = true
self.thumbnailImageView.layer.cornerRadius = 8
}
func update(with scene: Scene) {
}
private func setHighlighted(bool: Bool) {
if bool {
self.alpha = 0.5
} else {
self.alpha = 1.0
}
}
private func setSelected(bool: Bool) {
if bool {
self.thumbnailImageView.layer.borderWidth = 2.5
} else {
self.thumbnailImageView.layer.borderWidth = 0
}
}
}
在我的代码中,当 isSelected 设置为真时,我将图像视图的图层边框宽度更改为 2.5。
当我 select 一个单元格并滚动集合视图时,我认为单元格在重用 selected 单元格时保持 selected 状态,但重用单元格变为 unselected状态。其次,当我回到 selected 单元格并重新使用 unselected 单元格时,我认为它处于 unselected 状态。但是单元格自动设置 selected.
集合视图是否自动管理这些?
问题的代码运行良好。这是记录单元格选择并应用 selected/deselected 状态设置的替代解决方案。
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
//......
var selectedIndexPaths = [IndexPath]()
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndexPaths.append(indexPath)
}
public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let index = selectedIndexPaths.index(of: indexPath) {
selectedIndexPaths.remove(at: index)
}
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//...
cell.setSelected(selectedIndexPaths.contains(indexPath)) //Remember to make the cell's setSelected() public.
//...
}
//......
}