UITableViewCell 持有 UICollectionView 并点击集合单元格 segue
UITableViewCell holding UICollectionView and click collection cell segue
我正在使用 Swift3 和 Xcode 8.3.3
我正在研究持有 UICollectionView 的 UITableViewCell。
每个 CollectionView 单元格都需要执行 segue。
我的意思是,我需要用导航控制器调用其他 ViewController。所以用户可以回到 Main UITableViewController.
import UIKit
class PastCell: UITableViewCell {
@IBOutlet weak var ptripDtls: UICollectionView!
var logcount:Int = 0
override func awakeFromNib() {
super.awakeFromNib()
ptripDtls.delegate = self
ptripDtls.dataSource = self
logcount = Transfer.tripObj[Transfer.cellpos]["logcount"]! as! Int
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
let width = self.ptripDtls.collectionViewLayout.collectionViewContentSize.width;
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.itemSize = CGSize(width: width-17 , height: 50)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 1
ptripDtls!.collectionViewLayout = layout
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
extension PastCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return logcount
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PastTripCollectionCell", for: indexPath) as! PastTripCollectionCell
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//Might be here segue function will call
}
}
这里其实有多种选择:
关闭
当您对 tableView 单元格进行出列时,在 UITableViewCell
内传递一个闭包,例如 tapped: (Model) -> Void
,并在该闭包内指定执行 segue。 (如果您的所有收集单元将执行相同的转场,它会起作用,如果应该调用不同的转场,您可以使用此机制传播您的回调)
代表
定义一个自定义委托,通知您 viewController CollectoinView 的 DidSelectItem 方法,然后相应地执行 Segue。
通知
在整个系统中触发通知,只有您的 ViewController 会收听该通知,并相应地执行您的 segue。 (这实际上不是一个好的解决方案,只是指出它是可能的)
For swift 4 如果你正在使用 segue
,请在 didSelectRowAtIndexPath 中使用 perform segue
self.performSegue(withIdentifier: "YourSegueIdentifier", sender: nil)
如果您只使用导航控制器,请使用 Push。
let vc = self.storyboard?.instantiateViewController(withIdentifier: "YourVCIdentifier") as! YourVC
self.navigationController?.pushViewController(vc, animated: true)
我正在使用 Swift3 和 Xcode 8.3.3
我正在研究持有 UICollectionView 的 UITableViewCell。
每个 CollectionView 单元格都需要执行 segue。
我的意思是,我需要用导航控制器调用其他 ViewController。所以用户可以回到 Main UITableViewController.
import UIKit
class PastCell: UITableViewCell {
@IBOutlet weak var ptripDtls: UICollectionView!
var logcount:Int = 0
override func awakeFromNib() {
super.awakeFromNib()
ptripDtls.delegate = self
ptripDtls.dataSource = self
logcount = Transfer.tripObj[Transfer.cellpos]["logcount"]! as! Int
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
let width = self.ptripDtls.collectionViewLayout.collectionViewContentSize.width;
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.itemSize = CGSize(width: width-17 , height: 50)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 1
ptripDtls!.collectionViewLayout = layout
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
extension PastCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return logcount
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PastTripCollectionCell", for: indexPath) as! PastTripCollectionCell
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//Might be here segue function will call
}
}
这里其实有多种选择:
关闭
当您对 tableView 单元格进行出列时,在 UITableViewCell
内传递一个闭包,例如 tapped: (Model) -> Void
,并在该闭包内指定执行 segue。 (如果您的所有收集单元将执行相同的转场,它会起作用,如果应该调用不同的转场,您可以使用此机制传播您的回调)
代表
定义一个自定义委托,通知您 viewController CollectoinView 的 DidSelectItem 方法,然后相应地执行 Segue。
通知
在整个系统中触发通知,只有您的 ViewController 会收听该通知,并相应地执行您的 segue。 (这实际上不是一个好的解决方案,只是指出它是可能的)
For swift 4 如果你正在使用 segue
,请在 didSelectRowAtIndexPath 中使用 perform segueself.performSegue(withIdentifier: "YourSegueIdentifier", sender: nil)
如果您只使用导航控制器,请使用 Push。
let vc = self.storyboard?.instantiateViewController(withIdentifier: "YourVCIdentifier") as! YourVC
self.navigationController?.pushViewController(vc, animated: true)