【问题标题】:collection view arrays tied to index绑定到索引的集合视图数组
【发布时间】:2019-07-17 13:19:18
【问题描述】:

我正在制作一个三视图控制器项目,前两个视图是 collectionViews。选定的单元格将根据在第一个主视图上选择的索引加载带有 segue 和另一个数组的第二个视图控制器。谁能指出我如何将数据绑定到第二个集合视图中加载的正确方向。

import UIKit

class ViewController: UIViewController {

@IBOutlet private weak var collectionView: UICollectionView!


var collectionData = ["cell1", "cell2", "cell3", "cell4", "cell5", "cell6", "cell7", "cell8"]



override func viewDidLoad() {
    super.viewDidLoad()

    let height = (view.frame.size.width / 2.76)
    let width = view.frame.size.width / 1
    let layout = collectionView.collectionViewLayout as!
        UICollectionViewFlowLayout
    layout.itemSize = CGSize(width: width, height: height)


        }

    }

    extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return collectionData.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath)
    if let label = cell.viewWithTag(100) as? UILabel {
        label.text = collectionData[indexPath.row]

        }
    return cell
    }

    didSelectItemAt indexPath: IndexPath) {

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "BannerSegue" {
       if let _ = segue.destination as? BannerSelection, let _ = sender as? IndexPath {

      }
    }
  }
}

那是主视图控制器。那么它与这个观点有联系

import UIKit

class BannerSelection: UIViewController {


    @IBOutlet weak var collection2: UICollectionView!

    var BannerData = ["bannerA1", "bannerA2", "bannerA3", "bannerA4", "bannerA5", "bannerA6", "bannerA7", "bannerA8", "bannerA9", "bannerA10", "bannerA11", "bannerA12"]

    var selection: String!


    override func viewDidLoad() {
    super.viewDidLoad()
    let height = (view.frame.size.width / 3.76)
    let width = view.frame.size.width / 1
    let layout = collection2.collectionViewLayout as!
    UICollectionViewFlowLayout
    layout.itemSize = CGSize(width: width, height: height)

    }

}

    extension BannerSelection: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return BannerData.count
}

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let bannercell = collectionView.dequeueReusableCell(withReuseIdentifier: "BannerSelectionCell", for: indexPath)
    if let label = bannercell.viewWithTag(1) as? UILabel {
        label.text = BannerData[indexPath.row]
    }
    return bannercell
}
    func collectionView(_ BannerSelection: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }
}

所以我试图实现的是让我们说...索引 2 有一个数组“bannerB1,bannerB2”等等。然后我将对第二个视图控制器应用相同的技术,以使用结束 UIImage 填充最终视图。提前谢谢你!

【问题讨论】:

    标签: ios arrays swift populate collectionview


    【解决方案1】:

    由于我不清楚您的数据结构,因此您的数据似乎是这样的数组字典

    dict =    [
           "cell1" : [Banner1, Banner2,..]
           "cell2" : [Apple1, Apple2, ..]
    
           //so on 
    
        ]
    

    你的数据结构是什么结构,让它保存在UserDefaults中。

    UserDefaults.standard.setValue(dict, forKey: "myDataStructure")
    

    在第二个视图控制器中检索时

    let array = dict["cell1"] as? Array<String> // depends on the data structure 
    

    并填充您的 viewController。

    【讨论】:

      【解决方案2】:

      您可以将第一个视图的数据存储在 UserDefaults 中,然后从第二个视图中检索第一个 vc 的数据并加载它们

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-08
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 2012-01-16
        • 1970-01-01
        • 1970-01-01
        • 2022-01-08
        相关资源
        最近更新 更多