Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在tableview中执行segue集合视图_Swift_Xcode_Uitableview_Uicollectionview_Segue - Fatal编程技术网

Swift 在tableview中执行segue集合视图

Swift 在tableview中执行segue集合视图,swift,xcode,uitableview,uicollectionview,segue,Swift,Xcode,Uitableview,Uicollectionview,Segue,我将UICollectionview嵌入到UITableView中。 现在,我想在用户单击单元格时执行到新ViewController的切换 导入UIKit 类productsTableViewCell:UITableViewCell、UICollectionViewDataSource、UICollectionViewDelegate、UICollectionViewDelegateFlowLayout{ var imgArr = [ UIImage(named: "100000861

我将UICollectionview嵌入到UITableView中。 现在,我想在用户单击单元格时执行到新ViewController的切换

导入UIKit

类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) {

}
}

我无法执行到新视图控制器的切换 请帮帮我。 谢谢

您不能在UITableViewCell子类中使用用户performSegue,因为此方法仅在UIViewController中可用

对于您的问题有一个简单的解决方案-您可以将回调添加到单元格中,并在视图控制器中填充此回调

您的手机:

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
        }

我不能表演segue是什么意思?是否有任何错误消息或什么?您能否提供一些示例项目,例如github上的项目?当我使用performsguewithidentifier:identifier,sender:self时,在didSelectitemAt函数中说使用未解析的标识符“performsgue”时,您是否给了segue identifire?此方法对我有效,谢谢!