Dan*_*ond 2 arrays uicollectionview uicollectionviewcell swift swift3
我有一个UICollectionView有一堆单元格.当我选择这些单元格时,它们会改变颜色,看起来好像已经明确选择了,我将hashtag.hashtag_name(String)附加到我的hashtagsArray.如果我点击一个类别(时尚,食物,爱好或音乐),我会将另一个数组附加到该索引路径,以便为用户提供该特定类别的单元格,如下图所示.
我想要的是,如果我将已经SELECTED的单元格点击为UNSELECT它,那么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
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在数组上使用index(of:)方法来获取索引
if let index = hashtagArray.index(of: "SOMESTRING") {
hashtagArray.remove(at: index)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3290 次 |
最近记录: |