首页 \ 问答 \ Swift 3:从数组中删除特定的字符串而不知道indexPath?(Swift 3: Remove specific string from array without knowing the indexPath?)

Swift 3:从数组中删除特定的字符串而不知道indexPath?(Swift 3: Remove specific string from array without knowing the indexPath?)

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

我想要的是如果我点击一个已经SELECTED的单元格来取消选择,那么hashtag.hashtag_name就会从我的hashtagArray中移除。 问题是,当我将它添加到hashtagArray中时,我添加的数组的indexPath与数组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
            }
        }

    }

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

    }

原文:https://stackoverflow.com/questions/43211074
更新时间:2022-06-23 17:06

最满意答案

我最终为此问题创建了一个解决方法。 我在这里发布它可能会遇到这个问题的其他人。

我在监听click事件,当单击该元素时,隐藏控件,就像我想要全屏时一样。 然后,稍后(给双击时间),检查它是否是全屏,如果不是再次隐藏控件。 我必须暂时显示控件,因为一旦全屏显示,我无法隐藏或显示控件。

这是我使用的代码:

activeXElement.attachEvent('click',
    function(nButton){
        if(nButton!=1)return;// Not  left click

        // I can't set uiMode when full screen.
        // Set it now and set it back later if needed.
        activeXElement.uiMode='full';
        setTimeout(
            function(){
                if(activeXElement.fullScreen){
                    // It went full screen.
                    // Do some stuff...
                }
                else activeXElement.uiMode='none';
            }
        ,750);
    }
);

I ended up creating a workaround for this issue. I'm posting it here for others that may run into this problem.

I listen for the click event, and when the element is clicked, hide the controls like I wanted to when it went fullscreen. Then, later (give time for a double-click), check if it is full screen, and, if it isn't hide the controls again. I had to show the controls temporarily because I can't hide or show the controls once it's fullscreen.

Here is the code I used:

activeXElement.attachEvent('click',
    function(nButton){
        if(nButton!=1)return;// Not  left click

        // I can't set uiMode when full screen.
        // Set it now and set it back later if needed.
        activeXElement.uiMode='full';
        setTimeout(
            function(){
                if(activeXElement.fullScreen){
                    // It went full screen.
                    // Do some stuff...
                }
                else activeXElement.uiMode='none';
            }
        ,750);
    }
);

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)