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...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
最满意答案
我最终为此问题创建了一个解决方法。 我在这里发布它可能会遇到这个问题的其他人。
我在监听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); } );
相关问答
更多-
在IE中播放Windows Media对象时双击事件未触发(Double Click Event Not Fired on Playing Windows Media Object in IE)[2022-05-29]
我最终为此问题创建了一个解决方法。 我在这里发布它可能会遇到这个问题的其他人。 我在监听click事件,当单击该元素时,隐藏控件,就像我想要全屏时一样。 然后,稍后(给双击时间),检查它是否是全屏,如果不是再次隐藏控件。 我必须暂时显示控件,因为一旦全屏显示,我无法隐藏或显示控件。 这是我使用的代码: activeXElement.attachEvent('click', function(nButton){ if(nButton!=1)return;// Not left cli ... -
您的问题很可能是您的网址格式。 ns.play("rtmp://localhost/HelloWorld/media/sample.flv"); 这告诉Adobe Media Server加载名为media的HelloWorld应用程序的实例在Adobe媒体服务器中,连接时不传入任何媒体路径。 它在流文件夹中查找传递的媒体。 您还可以使用Application.xml文件(或主配置文件)添加其他目录以查找媒体。 最简单的方法是将媒体文件夹重命名为“streams”。 然后像这样连接: ns.play("r ...
-
当单元格上出现鼠标按下时, TextBox编辑控件获得焦点并接收其他鼠标的起伏,因此DataGrid双击事件不会引发。 由于DataGrid是只读的,因此可以将DataGridTextBoxColumn更改为不显示编辑控件。 这样双击事件就会提升。 为此,它足以覆盖Edit方法的重载并且不执行任何操作: public class MyDataGridTextBoxColumn : DataGridTextBoxColumn { protected override void Edit(Currenc ...
-
要对所有标签使用相同的单击事件: 在每个标签的属性中,转到Events (闪电标签)。 您将看到(可能在顶部附近) Click的标签,单击此事件的下拉列表,您将看到可用于该标签的处理程序列表。 这是属性>事件>单击处理程序(右下角): 因为所有标签都是相同的类型,并生成相同的EventArgs ,所以您可以对所有标签使用相同的处理程序。 然后,当您添加更多Label ,只需从Click事件下拉列表中选择事件处理程序: 希望这可以帮助! To use the same click event for all ...
-
一种方法是编写Windows Media Player插件。 在http://msdn.microsoft.com/en-us/library/bb262326(VS.85).aspx上有一个用于执行此操作的API。 以及有关如何在此处将插件向导更新到Visual Studio 2008的一些信息。 后台用户界面插件可以将您想要的信息(例如当前播放的曲目,标题等)写入您监视的文件。 UI插件只是一个实现IWMPPluginUI的对象。 One way to do it would be to write a ...
-
我没有运行Windows 8,所以我无法测试它,但你可以检查是否已经使用jQuery按下按钮。 例如:Html代码: jQuery的: var alreadySent = false; $(document).ready(function() { $("#myButton").click(function(event) { event.preventDefault(); if (!alreadySent) ...
-
从Windows Media Player(C#)检索当前播放的歌曲(Retrieving currently playing song from Windows Media Player (C#))[2023-08-02]
我想我可能找到了解决问题的方法 - 一个简单的解决方案是安装一个插件,告诉你当前播放的歌曲并将其写入文件,你可以通过你的程序阅读它。 I think I may have found a way around my problem- a simple solution is to install a plugin that tells you the current playing song and writes it to a file and you can read it through your p ... -
如果我做对了,你当前正在指定一个网站作为MediaElement的来源。 那不行。 如果您想嵌入YouTube内容,您有两种可能: 将网页嵌入WebView 将URL设置为视频本身作为Source If I get it right you are currently assigning a website as the source of the MediaElement. That can't work. If you want to embed youtube content you have two ...
-
对于C# 我花了好几个小时试图让它为我的程序工作。 最后我想通了。 此解决方案假设您在项目中的窗体上有Windows媒体播放器控件或通过代码创建了对象。 创建一个新的Windows Media Player Song对象并将其指向一个文件 创建一个字符串来保存您的艺术家,然后从歌曲对象中提取艺术家信息 创建一个字符串来保存您的标题,然后从歌曲对象中提取艺术家信息。 WMPLib.IWMPMedia song = wmp.newMedia(@"C:\SongName.mp3"); ...
-
当JQuery对话框关闭时,Windows Media Player继续播放(Windows Media Player continues to play when JQuery Dialog is closed)[2023-05-04]
我会建议像: dialog1.dialog({ beforeClose: function () { dialog1.html(""); }, draggable: true, width: 340, height: 105, resizable: false, closeOnEscape: true, modal: true, }); 要么: $( "#divPlayer" ).on( "dialogbeforeclose", function( event, u ...