gpt4 book ai didi

ios - 在swift中将 'n'号 Collection View 添加到UIStackView

转载 作者:行者123 更新时间:2023-11-30 11:30:04 44 4
gpt4 key购买 nike

我有一个 ViewController 类,我在其中放置 scrollView 并添加 stackView 作为 subview 。现在我想在 stackView 中添加 'n' 个 collectionView 。我正在从服务器获取数据,会有不同的类别。如果类别 A 有数据,那么我需要为其创建一个 collectionView。我需要在其类本身中处理相应的collectionView 的数据。

class ViewController: UIViewController {

var scrollView : UIScrollView!
var stackView : UIStackView!
var responseFullData : JsonFullDataResponse!
var responseData : JsonResponse!

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
print("View Controller init")
super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func viewDidAppear(_ animated: Bool) {
print("View did appear")
scrollView.contentSize = CGSize(width: stackView.frame.width, height: stackView.frame.height)
}

override func viewDidLoad() {
super.viewDidLoad()

//loading all the data
loadAllJsonData()

//setting up scroll and stack views
setUpScrollAndStackView()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("inside layout")
scrollView.contentSize = CGSize(width: stackView.frame.width, height: stackView.frame.height)
}

func setUpScrollAndStackView() {
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)

view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", options: .alignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", options: .alignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))

stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.spacing = 20.0
stackView.axis = .vertical
scrollView.addSubview(stackView)


scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[stackView]|", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["stackView": stackView]))
scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[stackView]", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["stackView": stackView]))

setUpOtherViewsInStack()
}

func setUpOtherViewsInStack() {
if responseFullData.banner?.count != 0 {
print("banner has data")

var bannerCollection : BannerCollectionView = BannerCollectionView() as! BannerCollectionView
bannerCollection.register(UINib.init(nibName: "BannerCell", bundle: nil), forCellWithReuseIdentifier: "BannerCell")
stackView.addArrangedSubview(bannerCollection)
}
}

BannerCollectionView 代码

class BannerCollectionView: UICollectionView , UICollectionViewDataSource{

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
print("control is in datasource")
return 5
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BannerCell", for: indexPath) as! BannerCell
cell.backgroundColor = UIColor.blue
return cell
}
}

我该如何进行这项工作。我应该在哪个类中以及哪里设置 BannerCollectionViewdelegatedatasource 属性?我如何初始化它?

最佳答案

据我了解,您需要从0开始for来计数responseFullData.banner.count每次添加collectionView

if let count = responseFullData.banner?.count {
for i in 0..<count {
var bannerCollection : BannerCollectionView = BannerCollectionView() as! BannerCollectionView
bannerCollection.register(UINib.init(nibName: "BannerCell", bundle: nil), forCellWithReuseIdentifier: "BannerCell")
stackView.addArrangedSubview(bannerCollection)
}
}

关于ios - 在swift中将 'n'号 Collection View 添加到UIStackView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50347812/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com