2

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...

IMAGE EXAMPLE

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
            }
        }

    }
Alladinian
  • 34,483
  • 6
  • 89
  • 91
Daniel Dramond
  • 1,538
  • 2
  • 15
  • 26
  • While you're using this to update a collection view, the question is really about arrays and indexes. Try and write a simple example in a playground, with the minimum code that demonstrates the problem. You may find this clarifies it for you (basically exclude anything to do with `UIKit`). Try and update your question to only show _relevant_ code. – Ashley Mills Apr 04 '17 at 15:13
  • @AshleyMills this is the relevant code? I only included the NSObject as it's what's passed in, the didSelectItemAtIndex path method as it requires a tap and the actual arrays...? I don't see the issue – Daniel Dramond Apr 04 '17 at 15:27
  • it might be relevant to your app, but it's not relevant to the core problem - how to remove a string from an array without knowing its index. That has nothing to do with collection views. Look at the answers given - neither of them mentions UICollectionViews. If you can distil your question down to the minimum viable example that demonstrates the issue, you'll help others help you and often the solution will present itself. See [mcve] – Ashley Mills Apr 04 '17 at 15:37

3 Answers3

9

You can use the index(of: ) method on the array to get the index

if let index = hashtagArray.index(of: "SOMESTRING") {
    hashtagArray.remove(at: index)
}
hawkstrider
  • 4,141
  • 16
  • 27
4
myArray = ["One","Two","Three","Four"]
myArray = myArray.filter{$0 != "Three"}

This will remove the "Three" from myArray. Also look at the following link too:

frogatto
  • 28,539
  • 11
  • 83
  • 129
Syed Hasnain
  • 130
  • 1
  • 8
1

You could use Array's index(of:) function to get the index of a known element and then remove it using remove(at:).

You could use filter(_:​) to create a new array excluding elements which match some check.

You could look at similar questions and write your own extension method to remove an object by value as in Array extension to remove object by value

Community
  • 1
  • 1
Jonah
  • 17,918
  • 1
  • 43
  • 70