我有两个集合视图。点击集合视图1中未选中的单元格将选择它们,并将所选单元格添加到集合视图2.在集合视图1(allHobbiesCV)中点击选定的单元格将取消选择它们并从集合视图2(myHobbiesCV)中删除它们。从本质上讲,它所做的只是切换。
集合视图2中的单元格也可以通过根据需要选择少量或多个来手动删除,然后按“删除”按钮。按钮。此过程非常有效,除非Collection View 1中的单元格仍保持选中状态,即使该特定单元格已从Collection View 2中删除。
如果手动选择并从Collection View 2中删除了单元格,则如何使用“删除”按钮取消选择“集合视图1”中的单元格?
班级 -
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语句总是返回一个空数组。 if let check中没有任何内容打印出来。有没有办法使用myHobbiesArraySelected而不是indexPathsForSelectedItems? (因为我现在将选定的项目保存在数组中)以及在myHobbiesCV中手动删除的所有HobbiesCV中取消选择单元格的原始预期功能。
谢谢朋友们。
答案 0 :(得分:0)
更新: IndexPath符合Equatable
(请参阅@ Paulw11的评论)
比较 IndexPath
中的对象contains
但是即使indexPath
路径的实例具有相同的部分和行,对象本身也是不同的。
1。解决方案:比较值
将包含更改为比较节和行
我建议实施解决方案2
答案 1 :(得分:0)
存储选定的索引路径是个坏主意,因为它们会耦合到集合视图。如果存储选定的项,则始终可以使用数组中项的索引来确定相应的索引路径。您还可以从给定的索引路径快速确定项目。
在下面的代码中,我为您的基础项使用了类型{
{
"_id": "vehicle1",
"vehicleType": "cars",
"vehicleId": "car1",
"vehicleDetails": {
"_id": "car1",
"carBrand": "Audi",
"color": ""
}
},
{
"_id": "vehicle2",
"vehicleType": "cars",
"vehicleId": "car2",
"vehicleDetails": {
"_id": "car2",
"carBrand": "BMW",
"color": "white"
},
{
"_id": "vehicle3",
"vehicleType": "bikes",
"vehicleId": "bike1",
"vehicleDetails": {
"_id": "bike1",
"bikeBrand": "Audi",
"color": "red"
}
},
{
"_id": "vehicle4",
"vehicleType": "bikes",
"vehicleId": "bike2",
"vehicleDetails": {
"_id": "bike2",
"carBrand": "BMW",
"color": "white"
}
},
}
,但它可以是Item
或String
或任何对象类型。如果您使用自己的类或结构,请确保使其符合Int
和Equatable
。
Hashable