In a collection view I want that when a cell is tapped to have the background color changed to indicate highlighting. When that same cell is tapped again, turn the highlighting off. When another cell is tapped, un-highlight the previously highlighted cell and highlight the new one.
I pretty much have that working but it's all done manually in code. If there's a better way using delegate methods or something, please enlighten me.
The collection view is in a pop-up view. My problem is that when the pop-up view is closed and then reopened, the last selected cell is still highlighted.
I tried this, but no-joy.
choose_CollectionView_Outlet.indexPathsForSelectedItems?.forEach { choose_CollectionView_Outlet.deselectItem(at: $0, animated: false) }
I think it's because I am manually setting the background color to indicate highlight. If anybody has a solution for this, please share it.
extension Choose_Items_VC: UICollectionViewDelegate
{
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
if collectionView == collectionView_Outlet
{
// Filter Selection
let item = itemArray[indexPath.row].Item_Name
if let cell = collectionView.cellForItem(at: indexPath)
{
cell.backgroundColor = .lightGray
}
switch itemPicked
{
case K.AppFacing.type:
if item == type_Fld_Outlet.text
{
pickerDoneBtn_Outlet.isEnabled = false
pickerFld_Outlet.text = ""
newType_ID = 0
} else {
pickerFld_Outlet.text = item
newType_ID = itemArray[indexPath.row].ItemID
pickerDoneBtn_Outlet.isEnabled = true
}
case K.AppFacing.style:
if item == style_Fld_Outlet.text
{
pickerDoneBtn_Outlet.isEnabled = false
pickerFld_Outlet.text = ""
newType_ID = 0
} else {
pickerFld_Outlet.text = item
newType_ID = itemArray[indexPath.row].ItemID
pickerDoneBtn_Outlet.isEnabled = true
}
case K.AppFacing.venue:
if item == venue_Fld_Outlet.text
{
pickerDoneBtn_Outlet.isEnabled = false
pickerFld_Outlet.text = ""
newType_ID = 0
} else {
pickerFld_Outlet.text = item
newType_ID = itemArray[indexPath.row].ItemID
pickerDoneBtn_Outlet.isEnabled = true
}
default: break
}
} else if collectionView == choose_CollectionView_Outlet {
let cell = collectionView.cellForItem(at: indexPath)
if cell?.backgroundColor == .lightGray
{
cell?.backgroundColor = nil
// cell?.isSelected = false
// cell?.isHighlighted = false
add_Btn_Outlet.isEnabled = false
gItem_ID = 0
} else {
cell?.backgroundColor = .lightGray
// cell?.isSelected = true
// cell?.isHighlighted = true
gItem_ID = choose_Items_Array[indexPath.item].ItemID
add_Btn_Outlet.isEnabled = true
}
}
}
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath)
{
if let cell = collectionView.cellForItem(at: indexPath)
{
if collectionView == collectionView_Outlet
{
cell.backgroundColor = .lightGray
}
}
}
func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath)
{
if let cell = collectionView.cellForItem(at: indexPath)
{
if collectionView == collectionView_Outlet
{
cell.backgroundColor = nil
}
}
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath)
{
if let cell = collectionView.cellForItem(at: indexPath)
{
cell.backgroundColor = nil
}
}
}