我正在制作一个带有前两个视图的三视图控制器项目是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填充最终视图。谢谢你!
答案 0 :(得分:0)
您可以将第一个视图的数据存储在UserDefaults中,然后从第二个视图中检索第一个vc的数据并加载它们
答案 1 :(得分:0)
由于我不清楚您的数据结构,因此您的数据似乎是这样的数组字典
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。