Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Swift 3:在不知道索引XPath的情况下从数组中删除特定字符串?_Arrays_Swift_Swift3_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

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

Arrays Swift 3:在不知道索引XPath的情况下从数组中删除特定字符串?,arrays,swift,swift3,uicollectionview,uicollectionviewcell,Arrays,Swift,Swift3,Uicollectionview,Uicollectionviewcell,我有一个UICollectionView,它有一堆单元格。当我选择这些单元格时,它们会改变颜色,看起来好像它们已被清楚地选中,我会将hashtag.hashtag_名称(字符串)附加到我的hashtagsArray中。如果我点击一个类别(时尚、食品、爱好或音乐),我会将另一个数组附加到该索引路径,为用户提供特定类别的单元格,如下面的图像示例所示 我想要的是,如果我点击一个已经选择的单元格取消选择它,那么hashtag.hashtag_名称将从我的hashtagArray中删除。问题是,我添加的数

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

我想要的是,如果我点击一个已经选择的单元格取消选择它,那么hashtag.hashtag_名称将从我的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)
}
您可以使用数组函数获取已知元素的索引,然后使用
remove(at:)
将其删除

您可以使用创建一个新数组,排除与某些检查匹配的元素

您可以查看类似的问题,并编写自己的扩展方法来按值删除对象,如中所示

这将从
myArray
中删除
“三个”
。还可以查看以下链接:


当您使用它来更新集合视图时,问题实际上是关于数组和索引的。试着在操场上写一个简单的例子,用最少的代码来演示这个问题。您可能会发现这为您澄清了这一点(基本上排除了与
UIKit
有关的任何内容)。请尝试更新您的问题以仅显示相关代码。@AshleyMills这是相关代码吗?我只包括了NSObject,因为它是传入的,didSelectItemAtIndex路径方法,因为它需要一个tap和实际数组。。。?我不认为这个问题可能与你的应用程序有关,但与核心问题无关——如何在不知道其索引的情况下从数组中删除字符串。这与集合视图无关。看看给出的答案——它们都没有提到UICollectionViews。如果你能把你的问题提炼到能证明问题的最简单可行的例子,你就会帮助别人帮助你,而且通常解决方案也会呈现出来。看见
myArray = ["One","Two","Three","Four"]
myArray = myArray.filter{$0 != "Three"}