I have a UICollectionView that has a bunch of cells. When I select these cells, they change color to look as if they have clearly been selected and I append that hashtag.hashtag_name (String) to my hashtagsArray. If I tap a category (fashion, food, hobbies or music), I append another array to that index path to give the user the cells for that specific category as you can see in my image example below.
What I would like is if I tap a already SELECTED cell to UNSELECT it, for that hashtag.hashtag_name to be removed from my hashtagArray. The issue is that the indexPath for the array that I add in is completely different to the array indexPath when I append it into the hashtagArray so I cannot remove it by calling self.hashtagArray.remove(Int)
. Here's my code...
import UIKit
class Hashtag: NSObject {
var hashtag_name: String?
var hashtag_color: String?
}
import UIKit
private let reuseIdentifier = "Cell"
class HashtagView: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var hashtagArray: [String] = []
var categoriesArray = [Hashtag]()
var fashionArray = [Hashtag]()
var isFashionSelected: Bool = false
var fashionArrayCount: [Int] = []
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = .white
navigationItem.title = "Hashtag"
self.collectionView?.backgroundColor = .white
self.collectionView?.register(HashtagCell.self, forCellWithReuseIdentifier: reuseIdentifier)
self.collectionView?.contentInset = UIEdgeInsetsMake(10, 0, 0, 0)
handleFetchCategories()
handleFetchFashionHashtags()
}
func insertCategoryAtIndexPath(element: [Hashtag], index: Int) {
categoriesArray.insert(contentsOf: element, at: index)
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = self.collectionView?.cellForItem(at: indexPath) as! HashtagCell
let hashtag = categoriesArray[indexPath.item]
if hashtag.hashtag_name == "FASHION" && isFashionSelected == false {
self.isFashionSelected = true
self.insertCategoryAtIndexPath(element: self.fashionArray, index: indexPath.item + 1)
self.collectionView?.reloadData()
} else if hashtag.hashtag_name == "FASHION" && isFashionSelected == true {
self.isFashionSelected = false
self.categoriesArray.remove(at: self.fashionArrayCount)
self.collectionView?.reloadData()
}
if hashtag.hashtag_name != "FASHION" && hashtag.hashtag_name != "FOOD" && hashtag.hashtag_name != "HOBBIES" && hashtag.hashtag_name != "MUSIC" {
if cell.isCellSelected == false {
cell.isCellSelected = true
if self.hashtagArray.contains(hashtag.hashtag_name!) {
cell.backgroundColor = .white
cell.hashtagLabel.textColor = greenColor
cell.layer.borderColor = greenColor.cgColor
} else {
self.hashtagArray.append(hashtag.hashtag_name!)
}
cell.backgroundColor = .white
cell.hashtagLabel.textColor = greenColor
cell.layer.borderColor = greenColor.cgColor
} else if cell.isCellSelected == true {
cell.isCellSelected = false
// REMOVE UNSELECTED CELL FROM ARRAY.
cell.backgroundColor = greenColor
cell.hashtagLabel.textColor = .white
cell.layer.borderColor = greenColor.cgColor
}
}
}