使用按钮取消选择集合视图中的单元格 [英] Using a button to deselect cells in a collectionview
问题描述
我有两个收藏夹视图。在收藏夹视图1中点击未选择的单元格将其选中,并将所选单元格添加到收藏夹视图2中。在收藏夹视图1(allHobbiesCV)中点击所选单元格将取消选择它们并将其从收藏夹视图2(myHobbiesCV)中删除。本质上,它所做的只是切换。
I have two collection views. Tapping unselected cells in Collection View 1 selects them, and adds the selected cell to Collection View 2. Tapping on selected cells in Collection View 1 (allHobbiesCV) will unselect them and remove them from Collection View 2 (myHobbiesCV). Essentially, all it's doing is toggling.
您还可以手动删除Collection View 2中的单元格,方法是根据需要选择任意数量,然后按删除按钮。此过程的效果很好,除了即使Collection View 1中的单元格仍保持选中状态也是如此,即使该特定单元格已从Collection View 2中删除了。
Cells in Collection View 2 can also be manually removed by selecting as few or many as desired, then pressing a 'Remove' button. This process works great, except the cells in Collection View 1 still remain selected, even if that particular cell was removed from Collection View 2.
如何使用
类级别-,如果要手动从集合视图2中删除单元格,则从删除按钮中取消选择它们即可。 >
Class level -
var allHobbiesArray = [String]()
var allHobbiesArraySelected = [String]()
var myHobbiesArray = [String]()
var myHobbiesArraySelected = [String]()
didSelectItemAt
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// All Hobbies
if collectionView == allHobbiesCV {
let item = allHobbiesArray[indexPath.item]
let cell = allHobbiesCV.cellForItem(at: indexPath) as! AllHobbiesCell
if let itemIndex = allHobbiesArraySelected.index(of:item) {
// DID DESELECT
allHobbiesArraySelected.remove(at:itemIndex)
myHobbiesArray.remove(at: itemIndex)
myHobbiesCV.deleteItems(at: [IndexPath(item: itemIndex, section:0)])
cell.backgroundColor = UIColor.brown
}
else {
// DID SELECT
allHobbiesArraySelected.insert(item, at: 0)
myHobbiesArray.insert(item, at: 0)
myHobbiesCV.insertItems(at: [IndexPath(item: 0, section:0)])
cell.backgroundColor = UIColor.green
}
allHobbiesCV.deselectItem(at: indexPath, animated: false)
for cell in myHobbiesCV.visibleCells {
cell.backgroundColor = UIColor.white
}
myHobbiesArraySelected.removeAll()
//myHobbiesCV.reloadData() // needed?
}
// My Hobbies
else {
let item = myHobbiesArray[indexPath.item]
let cell = myHobbiesCV.cellForItem(at: indexPath) as! MyHobbiesCell
if let itemIndex = myHobbiesArraySelected.index(of:item) {
// DID DESELECT
myHobbiesArraySelected.remove(at: itemIndex)
cell.backgroundColor = UIColor.white
}
else {
// DID SELECT
myHobbiesArraySelected.insert(item, at: 0)
cell.backgroundColor = UIColor.red
}
myHobbiesCV.deselectItem(at: indexPath, animated: true)
}
}
cellForItem
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == allHobbiesCV {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ALL", for: indexPath) as! AllHobbiesCell
cell.allHobbiesCellLabel.text = allHobbiesArray[indexPath.item]
let allHobbies = allHobbiesArray[indexPath.item]
if allHobbiesArraySelected.index(of: allHobbies) != nil {
cell.backgroundColor = UIColor.green
}
else {
cell.backgroundColor = UIColor.brown
}
return cell
}
else { // myHobbiesCV
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MY", for: indexPath) as! MyHobbiesCell
cell.myHobbiesCellLabel.text = myHobbiesArray[indexPath.item]
let myHobbies = myHobbiesArray[indexPath.item]
if myHobbiesArraySelected.index(of: myHobbies) != nil {
cell.backgroundColor = UIColor.red
}
else {
cell.backgroundColor = UIColor.white
}
return cell
}
}
numberOfItemsInSection
if collectionView == allHobbiesCV {
return allHobbiesArray.count
}
else {
return myHobbiesArray.count
}
删除按钮
@IBAction func deleteHobbyButtonPressed(_ sender: UIButton) {
print("all Hobbies - \(allHobbiesCV.indexPathsForSelectedItems!)")
if let selectedItemPaths = myHobbiesCV.indexPathsForSelectedItems {
var allItemIndexPaths = [IndexPath]()
var tempSelectedItems = Array(allHobbiesArraySelected) // Need to use a temporary copy otherwise the element indexes will change
for itemPath in selectedItemPaths {
let removeItem = allHobbiesArraySelected[itemPath.item]
if let removeIndex = tempSelectedItems.index(of: removeItem) {
print("if let removeIndex")
tempSelectedItems.remove(at: removeIndex)
}
if let allItemsIndex = allHobbiesArray.index(of: removeItem) {
print("if let allItemsIndex")
allItemIndexPaths.append(IndexPath(item: allItemsIndex, section: 0))
}
}
allHobbiesArraySelected = tempSelectedItems // Selected items array without the removed items
myHobbiesCV.deleteItems(at:selectedItemPaths)
myHobbiesCV.reloadData()
allHobbiesCV.reloadItems(at: allItemIndexPaths) // Reload to update the selected status
}
}
现在的问题是删除按钮没有任何评估。第一条print语句始终返回一个空数组。如果让我们检查,什么也不会打印。有没有一种方法可以使用myHobbiesArraySelected代替indexPathsForSelectedItems? (因为我现在将选定的项保存在数组中),以及取消原来在allHobbiesCV中选择单元格的预期功能,如果它是在myHobbiesCV中手动删除的话。
The problem now is nothing is evaluating in the remove button. That first print statement always returns an empty array. And nothing prints in the if let check. Is there a way to use myHobbiesArraySelected instead of indexPathsForSelectedItems? (Since I'm saving the selected items in an array now) Along with the original intended functionality of deselecting the cell in allHobbiesCV if it was manually deleted in myHobbiesCV.
谢谢
推荐答案
存储选定的索引路径是一个坏主意,因为这些路径与集合视图耦合。如果您存储所选的 items ,则始终可以使用数组中的项目索引来确定适当的索引路径。您还可以从给定的索引路径快速确定项目。
It is a bad idea to store selected index paths, as these are coupled to the collection view. If you store the selected items then you can always determine the appropriate index path, using the item's index in the array. You can also determine an item quickly from a given index path.
在下面的代码中,我使用了类型 Item
作为基础项目,但可以是 String
或 Int
或任何对象类型。如果您使用自己的类或结构,请确保使其符合 Equatable
和 Hashable
。
In the code below I have used the type Item
for your underlying item, but it could be String
or Int
or any object type. If you use your own class or struct, ensure you make it conform to Equatable
and Hashable
.
var allItems:[Item]
var selectedItems[Item]
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == allItemsCV {
let item = allItems[IndexPath.row]
if let itemIndex = selectedItems.index(of:item) {
selectedItems.remove(at:itemIndex)
selectedItemsCV.deleteItems(at: [IndexPath(item: itemIndex, section:0)])
} else {
selectedItems.insert(item, at: 0)
selectedItemsCV.insertItems(at: [IndexPath(item: 0, section:0)])
}
allItemsCV.deselectItem(at: indexPath, animated: true)
// cell for item at indexPath needs to render the selected/deselected
state correctly based on the item being in the selectedItems array
allItemsCV.reloadItems(at: [indexPath])
}
}
@IBAction func removeButtonPressed(_ sender: UIButton) {
//...
if let selectedItemPaths = selectedItemCV.indexPathsForSelectedItems {
var allItemIndexPaths = [IndexPath]()
var tempSelectedItems = Array(selectedItems) // Need to use a temporary copy otherwise the element indexes will change
for itemPath in selectedItemPaths {
let removeItem = selectedItems[itemPath.item]
if let removeIndex = tempSelectedItems.index(of: removeItem) {
tempSelectedItems.remove(at: removeItem)
}
if let allItemsIndex = allItems.index(of: removeItem) {
allItemIndexPaths.append(IndexPath(item: allItemsIndex, section: 0))
}
}
selectedItems = tempSelectedItems // Selected items array without the removed items
selectedItemsCV.deleteItems(at:selectedItemPaths)
allItemsCV.reloadItems(at: allItemIndexPaths) // Reload to update the selected status
}
}
这篇关于使用按钮取消选择集合视图中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!