У меня есть 3 контроллера представления с именем firstvc, secondvc, thirdvc
. И у меня есть один вид коллекции, который будет прокручиваться горизонтально. Если я выберу свой first cell
, мой firstvc
отобразится в моем дополнительном представлении на моем mainviewcontroller
. То же самое и для 2, 3rd cell
. Это отлично .
Теперь мне нужна одна функция, например, если я проведу пальцем по subview
, у которого будут все мои контроллеры представления, когда я выбираю любую ячейку. Мне нужно добавить свайп влево или вправо.
Например :
если я нахожусь в firstvc
, то мой first cell
будет выбран. Теперь, если я проведу subview
вправо, мой secondvc
должен появиться, в то время как моя вторая ячейка представления коллекции также должна highlight
Я не знаю, как применить.
мой код:
@IBOutlet weak var collectionview: UICollectionView!
@IBOutlet weak var contentSubView: UIView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.items.count
}
// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.myLabel.text = self.items[indexPath.item]
// cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
var borderColor: UIColor! = UIColor.clear
//var borderWidth: CGFloat = 0
if indexPath == selectedIndexPath{
borderColor = UIColor.red
//borderWidth = 1 //or whatever you please
}else{
borderColor = UIColor.white
// borderWidth = 0
}
cell.myview.backgroundColor = borderColor
// cell.myview.layer.borderWidth = borderColor
return cell
}
// MARK: - UICollectionViewDelegate protocol
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.item)!")
//based on your comment remove the subviews before add on your myview
//let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell
selectedIndexPath = indexPath
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)
}
Полный ZIP-файл моего проекта: https://www.dropbox.com/s/fwj745kdgqvgjxa/Collection%20view%20example.zip?dl=0