【问题标题】:Swift 3: Remove specific string from array without knowing the indexPath?Swift 3:在不知道 indexPath 的情况下从数组中删除特定字符串?
【发布时间】:2017-04-04 15:09:39
【问题描述】:

我有一个 UICollectionView 有一堆单元格。当我选择这些单元格时,它们会改变颜色,看起来好像它们已被明确选中,然后我将该 hashtag.hashtag_name (String) 附加到我的 hashtagsArray。如果我点击一个类别(时尚、食物、爱好或音乐),我会将另一个数组附加到该索引路径,以向用户提供该特定类别的单元格,如下面的图片示例所示。

我想要的是,如果我点击一个已经选择的单元格来取消选择它,以便从我的 hashtagArray 中删除那个 hashtag.hashtag_name。问题是我添加的数组的 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
            }
        }

    }

【问题讨论】:

  • 当您使用它来更新集合视图时,问题实际上与数组和索引有关。试着在操场上写一个简单的例子,用最少的代码来演示这个问题。您可能会发现这为您澄清了它(基本上排除了与UIKit 有关的任何事情)。尝试更新您的问题以仅显示相关代码。
  • @AshleyMills 这是相关代码吗?我只包括了 NSObject,因为它是传入的,didSelectItemAtIndex 路径方法,因为它需要点击和实际的数组......?我没有看到问题
  • 它可能与您的应用程序相关,但与核心问题无关 - 如何在不知道其索引的情况下从数组中删除字符串。这与集合视图无关。看看给出的答案——他们都没有提到 UICollectionViews。如果您可以将您的问题提炼为展示该问题的最小可行示例,那么您将帮助其他人帮助您,并且通常解决方案会自行呈现。见minimal reproducible example

标签: arrays swift swift3 uicollectionview uicollectionviewcell


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    myArray = ["One","Two","Three","Four"]
    myArray = myArray.filter{$0 != "Three"}
    

    这将从myArray 中删除"Three"。也请查看以下链接:

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2019-11-20
        • 2014-01-02
        • 2013-03-24
        • 1970-01-01
        • 2019-04-04
        • 2017-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多