在 tableview 中执行 segue collection view
perform segue collection view inside a tableview
我有嵌入到 UITableView 中的 UICollectionview。
现在我想在用户点击一个单元格
时执行一个新的 ViewController 的 segue
导入 UIKit
class productsTableViewCell:UITableViewCell、UICollectionViewDataSource、UICollectionViewDelegate、UICollectionViewDelegateFlowLayout {
var imgArr = [
UIImage(named: "1000008614"),
UIImage(named: "1000008621"),
UIImage(named: "1000008629")
]
var imgName = [
"iPhone XS",
"iPhone XS Max",
"iPhone 11"
]
var imgPrice = [
"15,000,000 T",
"17,000,000 T",
"21,000,000 T"
]
@IBOutlet weak var btnShowAll: UIButton!
@IBOutlet weak var lblSectionName: UILabel!
@IBOutlet weak var ProductsCollectionVIew: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
ProductsCollectionVIew.delegate = self
ProductsCollectionVIew.dataSource = self
ProductsCollectionVIew.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imgArr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Products", for: indexPath) as! ProductCollectionViewCell
cell.imgProduct.image = imgArr[indexPath.row]
cell.lblProductName.text = imgName[indexPath.row]
cell.lblPrice.text = imgPrice[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//here I can't use self.performSegue(withIdentifier: "identifier", sender: self)
}
@IBAction func btnShowAllAction(_ sender: Any) {
}
}
我无法执行 segue 到新的视图控制器
请帮我。
谢谢
您不能在 UITableViewCell
子类中使用 performSegue()
,因为此方法仅在 UIViewController
中可用。
您的问题有一个简单的解决方案 - 您可以将 "callback" 添加到单元格并在视图控制器中填充此回调。
你的手机:
class ProductsTableViewCell: UITableViewCell {
var didSelectItemAction: ((IndexPath) -> Void)?
//...
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
didSelectItemAction?(indexPath)
}
}
视图控制器中的代码:
class ViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath)
guard let productsCell = cell as? ProductsTableViewCell else {
return cell
}
productsCell.didSelectItemAction = { [weak self] indexPath in
self?.performSegue(withIdentifier: "yourID", sender: self)
}
return productsCell
}
}
如果您从上面给出的代码中收到警告,您可以简单地这样做:
let Tablecell = MyTableview.dequeueReusableCell(withIdentifier: MyCellID, for: indexPath) as! MyTableViewCell
Tablecell.configureCell() // this is function which setups my collection view
let collectionCell = Tablecell
collectionCell.didSelectItemAction = { [weak self] indexPath in
self?.performSegue(withIdentifier: "SegueID", sender: self)
}
return collectionCell
}
我有嵌入到 UITableView 中的 UICollectionview。 现在我想在用户点击一个单元格
时执行一个新的 ViewController 的 segue导入 UIKit
class productsTableViewCell:UITableViewCell、UICollectionViewDataSource、UICollectionViewDelegate、UICollectionViewDelegateFlowLayout {
var imgArr = [
UIImage(named: "1000008614"),
UIImage(named: "1000008621"),
UIImage(named: "1000008629")
]
var imgName = [
"iPhone XS",
"iPhone XS Max",
"iPhone 11"
]
var imgPrice = [
"15,000,000 T",
"17,000,000 T",
"21,000,000 T"
]
@IBOutlet weak var btnShowAll: UIButton!
@IBOutlet weak var lblSectionName: UILabel!
@IBOutlet weak var ProductsCollectionVIew: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
ProductsCollectionVIew.delegate = self
ProductsCollectionVIew.dataSource = self
ProductsCollectionVIew.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imgArr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Products", for: indexPath) as! ProductCollectionViewCell
cell.imgProduct.image = imgArr[indexPath.row]
cell.lblProductName.text = imgName[indexPath.row]
cell.lblPrice.text = imgPrice[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//here I can't use self.performSegue(withIdentifier: "identifier", sender: self)
}
@IBAction func btnShowAllAction(_ sender: Any) {
}
}
我无法执行 segue 到新的视图控制器 请帮我。 谢谢
您不能在 UITableViewCell
子类中使用 performSegue()
,因为此方法仅在 UIViewController
中可用。
您的问题有一个简单的解决方案 - 您可以将 "callback" 添加到单元格并在视图控制器中填充此回调。
你的手机:
class ProductsTableViewCell: UITableViewCell {
var didSelectItemAction: ((IndexPath) -> Void)?
//...
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
didSelectItemAction?(indexPath)
}
}
视图控制器中的代码:
class ViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath)
guard let productsCell = cell as? ProductsTableViewCell else {
return cell
}
productsCell.didSelectItemAction = { [weak self] indexPath in
self?.performSegue(withIdentifier: "yourID", sender: self)
}
return productsCell
}
}
如果您从上面给出的代码中收到警告,您可以简单地这样做:
let Tablecell = MyTableview.dequeueReusableCell(withIdentifier: MyCellID, for: indexPath) as! MyTableViewCell
Tablecell.configureCell() // this is function which setups my collection view
let collectionCell = Tablecell
collectionCell.didSelectItemAction = { [weak self] indexPath in
self?.performSegue(withIdentifier: "SegueID", sender: self)
}
return collectionCell
}