【问题标题】:Swift 3: How to Insert Array of an NSObject to another Array [duplicate]Swift 3:如何将 NSObject 的数组插入另一个数组 [重复]
【发布时间】:2017-04-02 21:45:27
【问题描述】:

我有一个[Hashtag] 数组。 Hashtag NSObject 只保存了一个 hashtagName 的变量,即String?。一个数组是类别数组,另一个是时尚数组。我想要的是当我在我的 didSelectItemAtIndexPath 方法中点击某个 indexPath 时,我想将 fashionArray 插入到所选索引处的 categoriesArray 中。我遇到的问题是它需要一个字符串数组而不是一个标签数组。我如何实现这一目标?多谢你们。代码如下:

import UIKit

class Hashtag: NSObject {

    var hashtagName: String?
}

private let reuseIdentifier = "Cell"

class HashtagView: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var categoriesArray = [Hashtag]()

    var fashionArray = [Hashtag]()

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

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        if indexPath.item == 1 {
            // insert here
            self.categoriesArray.insert(fashionArray[indexPath.item].hashtagName, at: 1)
            print(categoriesArray)
        }

    } 

【问题讨论】:

  • 这不是your previous question的转贴吗?为什么还有一个问题?为什么不编辑你的另一个?
  • @rmaddy 我显然是一个新手,在回答帮助我的编程之后......为什么这样的工作值得?
  • 是否是新手无关紧要。这个网站以某种方式运作。本网站不接受为了获得更多关注而转发问题。您应该做的正确的事情是根据需要编辑您之前的问题以使其更清晰。

标签: arrays swift swift3 uicollectionview collectionview


【解决方案1】:

插入那个 更好的是使用dictionary>,其中key是你的indexpath.row,value是Hashtag所有对象的集合。

【讨论】:

  • 当你回答一个问题时,你应该解释你的答案背后的原因,并提供示例代码来支持它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 2014-04-04
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
相关资源
最近更新 更多