如何在选择 collectionv 视图单元格时显示不同的视图控制器
我有 3 个视图控制器,分别称为 firstvc
、secondvc
、thirdvc
.我有一个将水平滚动的集合视图.我已经这样做了.如果我选择任何单元格,它将打印它是哪个索引路径.没事,没问题.所以在我的 mainviewcontroller
我有一个将水平滚动的集合视图.还有一个 UIView
叫做 myview
.每当我按下任何单元格时,我都会得到它的 indexPath
.我需要在我的 mainviewcontroller
中将我的 3 个视图控制器显示为 myview
的子视图.
I have 3 view controllers called firstvc
, secondvc
, thirdvc
. And I have one collection view which will scroll horizontally. I have done that. And if I select any cell, it will print which index path it was. It's fine, no problem. So in my mainviewcontroller
I have one collection view which will scroll horizontally. And there is one UIView
called myview
. Whenever I press any cell, I get its indexPath
. I need to show my 3 view controllers as subviews of myview
in my mainviewcontroller
.
到目前为止我的代码:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
if indexPath.item == 0 {
let alertStoryBoard = UIStoryboard(name: "Main", bundle: nil)
if let allCollectionViewController = alertStoryBoard.instantiateViewController(withIdentifier:"firstvc") as? firstvc {
self.contentSubView.addSubview(allCollectionViewController)
} else if indexPath.item == 1 {
} else if indexPath.item == 2 {
}
}
}
我在这一行遇到错误:
self.contentSubView.addSubview(allCollectionViewController)
无法将firstvc"类型的值转换为预期的参数类型UIView"
我该如何解决这个问题.
How do i solve this issues.
提前致谢!!
如果 indexPath.item == 0 {
if indexPath.item == 0 {
let alertStoryBoard = UIStoryboard(name: "Main", bundle: nil)
if let allCollectionViewController = alertStoryBoard.instantiateViewController(withIdentifier:"firstvc") as? firstvc {
self.contentSubView.addSubview(allCollectionViewController.view)
} else if indexPath.item == 1 {
let alertStoryBoard = UIStoryboard(name: "Main", bundle: nil)
if let allCollec = alertStoryBoard.instantiateViewController(withIdentifier:"secondvc") as? secondvc {
self.contentSubView.addSubview(allCollec.view)
}else if indexPath.item == 2 {
let alertStoryBoard = UIStoryboard(name: "Main", bundle: nil)
if let wController = alertStoryBoard.instantiateViewController(withIdentifier:"Thirdvc") as? Thirdvc {
self.contentSubView.addSubview(wController.view)
}
只显示第一个vc类,不显示第二个和第三个.另外,为什么我关注 lk 这意味着.当我按下任何集合视图单元格时,该特定类视图控制器必须显示在我的 mainviewcontroller
Only showing first vc class alone, not showing second and third one. Also, why i am following lk this means. When ever i press on any collection view cell, that particular class view controlelr have to show in my sub view of uiview in my mainviewcontroller
在我的第一个视图控制器中,我放置了一个带有一些背景颜色的 uiview.但根本没有显示.它显示的是白色.那也没有正确显示
In my first view controller i placed one uiview with some background color .But that not at all showing .Its showing whitee color.That too not showing correctly
喜欢
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
based on your comment remove the subviews before add on your myview
for subview in contentSubView.subviews {
subview.removeFromSuperview()
}
let alertStoryBoard = UIStoryboard(name: "Main", bundle: nil)
var controller: UIViewController!
if indexPath.item == 0 {
if let allCollectionViewController = alertStoryBoard.instantiateViewController(withIdentifier:"firstvc") as? firstvc {
controller = allCollectionViewController
}
} else if indexPath.item == 1 {
if let allCollec = alertStoryBoard.instantiateViewController(withIdentifier:"secondvc") as? secondvc {
controller = allCollec
}
}else if indexPath.item == 2 {
if let wController = alertStoryBoard.instantiateViewController(withIdentifier:"Thirdvc") as? Thirdvc {
controller = wController
}
}
addChildViewController(controller)
// Add the child's View as a subview
contentSubView.addSubview(controller.view)
controller.view.frame = contentSubView.bounds
controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// tell the childviewcontroller it's contained in it's parent
controller.didMove(toParentViewController: self)
}