【发布时间】: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