草庐IT

arrays - swift 3 : Remove specific string from array without knowing the indexPath?

coder 2023-09-12 原文

我有一个包含一堆单元格的 UICollectionView。当我选择这些单元格时,它们会改变颜色,看起来就像它们已被清楚地选中一样,我将那个 hashtag.hashtag_name (String) 附加到我的 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)
}

关于arrays - swift 3 : Remove specific string from array without knowing the indexPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43211074/

有关arrays - swift 3 : Remove specific string from array without knowing the indexPath?的更多相关文章

随机推荐