我正在使用集合视图将对象添加到名为collectiveSelection
的数组中当选择集合视图单元格(currentSelection)时,我想测试该对象是否存在于数组(collectiveSelection)中。如果是,我想删除它(以实现切换开/关效果)。如果它不在数组中,我想添加它。
我添加到数组中的项目属于自定义类,因此在我的检查中尝试使用.index(of :)时会出现问题。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
currentSelection = itMachine.selection[indexPath.row] // returns some selection of type 'Attractions'
if collectiveSelection.contains(where: { $0 == currentSelection}){ // if selection exists in array, remove it, if it doesn't, add it
let itemIndex = collectiveSelection.index(of: currentSelection) // error message about .index not being available for custom class Attractions
collectiveSelection.remove(at: itemIndex)
updateCell(having: indexPath, selected: false)
} else { // deselect
collectiveSelection.append(currentSelection!)
updateCell(having: indexPath, selected: true)
}
}
我已经尝试使我的自定义类符合Equatable,但是这仍然没有帮助,因为我仍然遇到错误。关于如何在数组collectiveSelection中获取自定义元素currentSelection的索引的任何想法?
答案 0 :(得分:0)
您可能会发现跟踪所选索引更容易,而不是跟踪自定义对象。毕竟,您可以在数组中开始使用对象。
看看这个:
//
// TrackSelectedViewController.swift
//
// Created by Don Mag on 11/16/17.
//
import UIKit
private let reuseIdentifier = "TrackingCell"
class TrackSelectedViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var theCV: UICollectionView!
// this will maintain a list of IndexPath for selected cells
var collectiveSelection = [IndexPath]()
override func viewDidLoad() {
super.viewDidLoad()
theCV.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
// MARK: UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 30
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
// does collectiveSelection contain this indexPath?
let idx = collectiveSelection.index(of: indexPath) ?? -1
if idx > -1 {
// indexPath IS in collectiveSelection
cell.backgroundColor = .red
} else {
// indexPath IS NOT in collectiveSelection
cell.backgroundColor = .lightGray
}
// of course, you'll configure your cell other than just setting the background color
return cell
}
// MARK: UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// get a reference to the cell
let cell = collectionView.cellForItem(at: indexPath)
// does collectiveSelection contain this indexPath?
let idx = collectiveSelection.index(of: indexPath) ?? -1
if idx > -1 {
// indexPath IS in collectiveSelection
// so remove it and set the cell to Gray
collectiveSelection.remove(at: idx)
cell?.backgroundColor = .lightGray
// your code:
// updateCell(having: indexPath, selected: true)
} else {
// indexPath IS NOt in collectiveSelection
// so add it and set the cell to Red
collectiveSelection.append(indexPath)
cell?.backgroundColor = .red
// your code:
// updateCell(having: indexPath, selected: true)
}
}
}
如果您创建一个新的UIViewController
并添加UICollectionView
(并将其连接到IBOutlet和DataSource以及Delegate),您可以按原样运行该代码...它将切换单元格灰色和红色之间,取决于它们是否在collectiveSelection