gpt4 book ai didi

swift - UICollectionView 中的 ScrollToItem 没有发生任何事情

转载 作者:行者123 更新时间:2023-11-28 15:34:18 24 4
gpt4 key购买 nike

我在 ViewController 中有两个 Collection View 。一个 Collection View 代表顶部的标签栏,另一个代表屏幕下半部分的页面。我在自定义 UIView(从另一个类加载)中有第二个 UICollectionView(在下半部分)。我的目标是当我点击标签栏时,根据 scrollToIndex 相应地移动底部的 collectionView。我的问题是,当我单击顶部的选项卡栏并调用自定义 UIView 中的函数时,没有任何反应。我需要通过委托(delegate)/协议(protocol)共享信息吗?我做错了什么?

在我的 MainVC 中我有:

/** Setting up the top header **/


let topBar = UIView()
/** Setting up the connection to the Bottom Collection View **/
let mainView = MainViewsHome()

override func viewWillAppear(_ animated: Bool) {
self.view.layoutIfNeeded()
/**Setting up the header that the buttons lay on **/
topBar.frame = CGRect(x:self.view.frame.width * 0, y:self.view.frame.height * 0, width:self.view.frame.width,height:self.view.frame.height / 6.2)
topBar.backgroundColor = UIColor.red
self.view.addSubview(topBar)

/** Setting up the buttons in the header**/
setUpHeaderBarButtons()

/** Setting up the bottom half **/
mainView.frame = CGRect(x:self.view.frame.width * 0, y:self.view.frame.height / 6.2, width:self.view.frame.width,height:self.view.frame.height / 1.1925)
mainView.backgroundColor = UIColor.clear
self.view.addSubview(mainView)
mainView.delegate = self

}

& 然后为 MainVC(条形按钮)设置 collectionView

func setUpHeaderBarButtons() {
let flowLayout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: CGRect(x: topBar.frame.width * 0, y:topBar.frame.height / 1.75, width: topBar.frame.width, height: topBar.frame.height / 3.6), collectionViewLayout: flowLayout)
collectionView.register(SelectionCVC.self, forCellWithReuseIdentifier: cellId)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = UIColor.clear
topBar.addSubview(collectionView)

}

&& 然后是 MainVC 的 didSelect(条形按钮)

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("selecting cell")
mainView.moveToPage()
}

&& 然后我的自定义 UIVIew 和我希望触发 scrollToIndex 的底部 Collection View

class MainViewsHome: UIView, UIGestureRecognizerDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

var cellId = "Cell"

var collectionView2 : UICollectionView!

override init(frame: CGRect) {

super.init(frame: CGRect(x:0, y:0, width:UIScreen.main.bounds.width, height: UIScreen.main.bounds.height / 1.27))

/**Creation of the View **/
let flowLayout2 : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
flowLayout2.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
flowLayout2.scrollDirection = .horizontal

let collectionView2 = UICollectionView(frame: CGRect(x:self.frame.width * 0,y:self.frame.height * 0,width:self.frame.width,height: self.frame.height), collectionViewLayout: flowLayout2)
collectionView2.register(SelectionCVC.self, forCellWithReuseIdentifier: cellId)
collectionView2.isPagingEnabled = true
collectionView2.delegate = self
collectionView2.dataSource = self
collectionView2.backgroundColor = UIColor.purple

self.addSubview(collectionView2)

}


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

func moveToPage() {
// print("we are moving to page")

let indexPath = IndexPath(item: 2, section: 0) //Change section from 0 to other if you are having multiple sections
self.collectionView2?.scrollToItem(at: indexPath, at: [], animated: true)

}
}

我哪里错了?

最佳答案

MainViewsHomeinit 中,您正在初始化全新的 collectionView 而不是初始化实例属性 collectionView2 所以你在 collectionView2 中没有引用。

应该是

self.collectionView2 = UICollectionView(frame: CGRect(x:self.frame.width * 0,y:self.frame.height * 0,width:self.frame.width,height: self.frame.height), collectionViewLayout: flowLayout2)

代替

let collectionView2 = UICollectionView(frame: CGRect(x:self.frame.width * 0,y:self.frame.height * 0,width:self.frame.width,height: self.frame.height), collectionViewLayout: flowLayout2)

关于swift - UICollectionView 中的 ScrollToItem 没有发生任何事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44457157/

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