我在集合視圖中遇到了方法“didDeselectItemAt”的問題。應用程式:我有一個帶有單元格的集合(見螢屏截圖)。用戶只能突出顯示一個單元格。
當用戶單擊一個單元格時,它會突出顯示。當他點擊另一個時,另一個被選中,前一個變為正常。
例如,當我選擇第一個單元格,然后選擇最后一個單元格時,第一個單元格保持選中狀態。憑經驗,我發現了這個問題。發生在第一個之后,我在單擊第一個時單擊了一個看不見的單元格(您需要滾動)
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == albumsCollectionView {
guard let cell = collectionView.cellForItem(at: indexPath) as? AlbumCollectionCell else { return }
cell.isCellSelected()
presenter?.didChooseAlbum(with: indexPath.row) {
self.picturesCollectionView.reloadData()
}}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if collectionView == albumsCollectionView {
guard let cell = collectionView.cellForItem(at: indexPath) as? AlbumCollectionCell else { return }
cell.isCellDeselected()
}
}

uj5u.com熱心網友回復:
正如評論中提到的@DonMag,您必須跟蹤資料源中的選定狀態。以下是步驟。
當您在單元格中顯示一些資料時,您可以為其制作模型。
struct Album {
let name: String
var isSelected: Bool
}
在AlbumCollectionCell添加Album型別變數。并覆寫的isSelected屬性UICollectionViewCell
class AlbumCollectionCell: UICollectionViewCell {
var album: Album? {
didSet {
guard let album = album else { return }
if shoe.isSelected {
self.backgroundColor = UIColor.orange
}
else {
self.backgroundColor = UIColor.green
}
self.albumLabel.textColor = .white
self.albumLabel.text = album.name
}
}
override var isSelected: Bool {
didSet {
if self.isSelected {
self.backgroundColor = UIColor.orange
}
else {
self.backgroundColor = UIColor.green
}
}
}
}
在中,創建一個包含單元格資料的型別UIViewController陣列。您可以在方法Album中分配資料。viewDidLoad()
var albums = [Album]()
將allowsSelection屬性設定UICollectionView為true。
collectionView.allowsSelection = true
在cellForItem方法中將Album資料傳遞給單元格。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AlbumCollectionCell", for: indexPath) as? AlbumCollectionCell {
cell.album = albums[indexPath.item]
return cell
}
return UICollectionViewCell()
}
修改 didSelectItem 方法,如下所示。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
albums[indexPath.item].isSelected = true
}
并覆寫didDeselectItemAt方法。
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
albums[indexPath.item].isSelected = false
}
uj5u.com熱心網友回復:
如果您嘗試保持選擇 - 也就是說,如果您想保存它并恢復應用程式使用之間的最后選擇,或者如果您正在導航并且想要在再次導航到該控制器時恢復它 - 那么您需要使用您的資料源對其進行跟蹤。
但是,如果您不關心持久性,則可以讓集合視圖處理它。
在您的單元格類中,覆寫isSelected:
override var isSelected: Bool {
didSet {
contentView.backgroundColor = isSelected ? .systemOrange : .systemGreen
}
}
現在,您不需要任何“單元格外觀”代碼didSelectItemAt...didDeselectItemAt集合視圖將管理所選狀態。
這是一個快速,完整的例子......
自定義單元類
class LabelCollectionViewCell: UICollectionViewCell {
let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
label.textColor = .white
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(label)
let g = contentView.layoutMarginsGuide
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: g.topAnchor, constant: 4.0),
label.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 8.0),
label.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -8.0),
label.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -4.0),
])
contentView.backgroundColor = .systemGreen
contentView.layer.cornerRadius = 12
}
override var isSelected: Bool {
didSet {
contentView.backgroundColor = isSelected ? .systemOrange : .systemGreen
}
}
}
示例控制器類
class SelColViewVC: UIViewController {
var albumsCollectionView: UICollectionView!
let myData: [String] = [
"Favorites",
"Recents",
"Recently Added",
"Something Else",
"Five",
"Six",
"Seven",
"Eight",
]
override func viewDidLoad() {
super.viewDidLoad()
let fl = UICollectionViewFlowLayout()
fl.scrollDirection = .horizontal
fl.estimatedItemSize = CGSize(width: 120, height: 50)
albumsCollectionView = UICollectionView(frame: .zero, collectionViewLayout: fl)
albumsCollectionView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(albumsCollectionView)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
albumsCollectionView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
albumsCollectionView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
albumsCollectionView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
albumsCollectionView.heightAnchor.constraint(equalToConstant: 80.0),
])
albumsCollectionView.contentInset = UIEdgeInsets(top: 0.0, left: 8.0, bottom: 0.0, right: 8.0)
albumsCollectionView.register(LabelCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
albumsCollectionView.dataSource = self
albumsCollectionView.delegate = self
}
}
extension SelColViewVC: UICollectionViewDataSource, UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return myData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let c = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! LabelCollectionViewCell
c.label.text = myData[indexPath.item]
return c
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == albumsCollectionView {
presenter?.didChooseAlbum(with: indexPath.item) {
self.picturesCollectionView.reloadData()
}}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/476589.html
上一篇:SwiftUI:如何使用0索引陣列鍵為JSON添加型別
下一篇:SwiftUI中的年份陣列
