ios - 从 Tableview 中的 CollectionCell 执行 segue
问题描述
如何从tableview中的collectioncell执行segue?我在tableview 中创建了一个collectionview,我想从collectioncell 到另一个veiwcontroller 执行segue。每个单元格都有自己的转场。例如,“ if indexpath.row = 0 如果 indexpath.row == 1 perferom segue 到标识符“B”,则执行到标识符“A”。谢谢,这是我的代码。
import UIKit
protocol SegueSelectionDelegate: class {
func didSelect()
}
class ProductCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
weak var delegate: SegueSelectionDelegate?
var productArray:[(names:String,image:String)] =
[]
@IBOutlet weak var collectionView: UICollectionView!{
didSet{
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.register(UINib(nibName: "CategoriesTwoCollCell", bundle: nil),
forCellWithReuseIdentifier: "cell")
self.collectionView.reloadData()
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
productArray.append((names: "Bicycles", image: "KIA"))
productArray.append((names: "Motorbikes", image: "Lotus"))
productArray.append((names: "Fabric Safe", image: "Mini"))
productArray.append((names: "gas", image: "Nissan"))
productArray.append((names: "gas", image: "Mercedes"))
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection
section: Int) -> Int {
return productArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CategoriesTwoCollCell
cell.titleProductlbl.text = self.productArray[indexPath.row].names
// cell.renlbl.text = self.productArray[indexPath.row].rent
cell.imageView.image = UIImage(named:self.productArray[indexPath.row].image)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 85, height: 90)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.didSelect()
}
}
class MainTableViewController: UIViewController,MainTableViewCellDelegate ,SegueSelectionDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"productcell1" , for: indexPath) as! ProductCell
cell.delegate = self
return cell
}
}
func didSelect() {
// how can i add indexpath.row as showen below it wont allow me to do it?
if indexpath.row == 0 {
performSegue(withIdentifier: "BicyclesOptionseg", sender: self)
} else {
if indexpath.row == 2 {
performSegue(withIdentifier: "CarsOptionseg", sender: self)
}
}
解决方案
要解决此问题,您只需将所需的参数添加到您的delegate
方法中即可。看起来你不需要整个IndexPath
,只需要行,所以这样做:
protocol SegueSelectionDelegate: class {
func didSelect(index: Int) // Can also change the parameter to IndexPath if you want but not necessary
}
然后在collectionView的didSelect方法中:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.didSelect(index: indexPath.row)
}
最后在 MainTableViewController 中:
class MainTableViewController: UIViewController, MainTableViewCellDelegate , SegueSelectionDelegate {
func didSelect(index: Int) {
// Now you have access to index and can use it
if index == 0 {
performSegue(withIdentifier: "BicyclesOptionseg", sender: self)
}
}
}
推荐阅读
- ios - 获取所有蓝牙配对设备的列表
- c# - ASP.NET MVC Html.CheckBoxListFor 丢失/编译器错误
- sql - SQL - 如果差异低于阈值,则合并两行
- android - 将时间戳转换为星期几android
- ios - Google OAuth 提示在 Ionic IOS 应用上显示为“prompt-none”
- html - CSS 相对位置,推送元素
- python - Tensorflow-text: NotFoundError: _text_similarity_metric_ops.so not found
- javascript - 为什么我的 Tampermonkey 脚本没有删除 html 元素的样式
- html - 为什么谷歌浏览器支持 URL 末尾的 @
- gpu - 如何知道机器使用的是哪个 GPU?