Swift 3:在不知道索引路径的情况下从数组中删除特定字符串?

Swift 3: Remove specific string from array without knowing the indexPath?

我有一个包含一堆单元格的 UICollectionView。当我 select 这些单元格时,它们会改变颜色,看起来好像它们已经被 select 编辑过,我将 hashtag.hashtag_name (字符串)附加到我的 hashtagsArray。如果我点击一个类别(时尚、食品、爱好或音乐),我会在该索引路径中附加另一个数组,为用户提供该特定类别的单元格,如下面的图片示例所示。

我想要的是,如果我点击一个已经选择的单元格以取消选择它,那么 hashtag.hashtag_name 将从我的 hashtagArray 中删除。问题是我添加的数组的 indexPath 与我将其附加到 hashtagArray 时的数组 indexPath 完全不同,因此我无法通过调用 self.hashtagArray.remove(Int) 将其删除。这是我的代码...

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

    }

你可以在数组上使用 index(of: ) 方法来获取索引

if let index = hashtagArray.index(of: "SOMESTRING") {
    hashtagArray.remove(at: index)
}

您可以使用 Array 的 index(of:) 函数获取已知元素的索引,然后使用 remove(at:).

将其删除

您可以使用 filter(_:​) 创建一个新数组,排除匹配某些检查的元素。

您可以查看类似的问题并编写自己的扩展方法来按值删除对象,如 Array extension to remove object by value

myArray = ["One","Two","Three","Four"]
myArray = myArray.filter{[=10=] != "Three"}

这将从 myArray 中删除 "Three"。也看看下面的link: