【发布时间】:2017-03-29 17:06:18
【问题描述】:
我有两个集合视图。点击集合视图 1 中未选择的单元格将选中它们,并将选定的单元格添加到集合视图 2。点击集合视图 1 (allHobbiesCV) 中的选定单元格将取消选择它们并将它们从集合视图 2 (myHobbiesCV) 中删除。本质上,它所做的只是切换。
Collection View 2 中的单元格也可以手动删除,方法是根据需要选择少或多,然后按“删除”按钮。这个过程效果很好,除了集合视图 1 中的单元格仍然保持选中状态,即使该特定单元格已从集合视图 2 中删除。
如果手动选择并从 Collection View 2 中删除单元格,如何使用删除按钮从 Collection View 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
}
}
现在的问题是删除按钮中没有任何评估。第一个打印语句总是返回一个空数组。 if let 检查中没有打印任何内容。有没有办法使用 myHobbiesArraySelected 而不是 indexPathsForSelectedItems? (因为我现在将所选项目保存在数组中)以及取消选择 allHobbiesCV 中的单元格的原始预期功能,如果它是在 myHobbiesCV 中手动删除的。
感谢朋友。
【问题讨论】:
标签: ios arrays swift uicollectionview