2017-04-04 35 views
1

我有一個UICollectionView有一堆單元格。當我選擇這些單元格時,它們將顏色更改爲看起來好像它們已被明確選中,並將該hashtag.hashtag_name(String)追加到我的hashtagsArray中。如果我點擊一個類別(時尚,食物,愛好或音樂),我將另一個數組附加到該索引路徑,以便爲用戶指定該特定類別的單元格,如下圖中的示例所示。Swift 3:從數組中刪除特定的字符串而不知道indexPath?

我想要的是如果我點擊一個已經SELECTED的單元格來取消選擇它,這個hashtag.hashtag_name將從我的hashtagArray中移除。問題是,當我將它添加到hashtagArray中時,我添加的數組的indexPath與數組indexPath完全不同,因此我無法通過調用self.hashtagArray.remove(Int)將其刪除。這裏是我的代碼...

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

    } 
+0

當你使用它來更新集合視圖,真正的問題是有關數組和索引。試着在操場上寫一個簡單的例子,用最少的代碼來證明問題。你可能會發現這爲你澄清了它(基本上排除了與'UIKit'有關的任何事情)。嘗試並更新您的問題以僅顯示_relevant_代碼。 –

+0

@AshleyMills這是相關的代碼?我只包含NSObject,因爲它傳入的是didSelectItemAtIndex路徑方法,因爲它需要一個tap和實際的數組... ...?我沒有看到問題 –

+0

它可能與您的應用程序相關,但它與核心問題無關 - 如何在不知道其索引的情況下從數組中刪除字符串。這與收集視圖無關。看看給出的答案 - 他們都沒有提到UICollectionViews。如果您可以將問題歸結爲證明問題的最小可行示例,那麼您可以幫助其他人幫助您,並且通常解決方案將呈現給您。見[mcve] –

回答

4

可以使用(作者:)索引陣列上的方法來獲取指數

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

謝謝,先生! –

相關問題