Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.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
Ios 在单元格内的集合视图中实现didSelect_Ios_Swift_Uitableview_Uicollectionview - Fatal编程技术网

Ios 在单元格内的集合视图中实现didSelect

Ios 在单元格内的集合视图中实现didSelect,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我试图从表视图单元格中的集合视图执行一个segue,该视图如下所示 为了更好地解释它,我有一个表视图,每行有一个单元格,在第二行中,我有一个集合视图,其中包括几个产品 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch indexPath.row { case 0: let cell = tableVi

我试图从表视图单元格中的集合视图执行一个segue,该视图如下所示

为了更好地解释它,我有一个表视图,每行有一个单元格,在第二行中,我有一个集合视图,其中包括几个产品

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.row {
    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath)
        return cell
    case 1:
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemTitle", for: indexPath)
        return cell
    case 2:
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
        return cell
    case 3:
        let cell = tableView.dequeueReusableCell(withIdentifier: "SectionTitle", for: indexPath)
        return cell
    case 4:
        let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
        return cell
    default:
        let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
        return cell
    }
}
在我的ItemCell类中,我有以下内容

@IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "singleItemCell", for: indexPath) as! singleItemCell

    cell.itemImage.image = images[indexPath.row]
    cell.itemImage.contentMode = .scaleAspectFit


    return cell
}
我想执行一个步骤,例如当用户从集合视图中选择一个单元格时,它会将他带到另一个视图控制器。我该怎么做

我尝试了performSegue,但它不允许我,因为我在cell类


请问有什么建议吗

实现
UICollectionViewDataSource
UICollectionViewDelegate
不是在tableView单元格中,而是在您的
ViewController
中。 您可以在
cellforrowatinexpath
方法中访问collectionView,因此您可以将其
dataSource
delegate
分配给
self

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.row {
    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath)
        return cell
    case 1:
        if let cell = tableView.dequeueReusableCell(withIdentifier: "ItemTitle", for: indexPath) as? ItemCell {
            cell.collectionView?.delegate = self
            cell.collectionView?.dataSource = self
            return cell
        }
    ...
    }
}

ViewController
中,您可以在ItemCell类中创建
performsgue
method

以如下方式创建引用ViewController的变量:

var productVC:UIVIewController?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.row {
    case 2:
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
        cell.productVC = self
        return cell
    default:
        let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
        return cell
    }
}
加载TableView单元格时,将引用设置为self,如下所示:

var productVC:UIVIewController?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.row {
    case 2:
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
        cell.productVC = self
        return cell
    default:
        let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
        return cell
    }
}
然后在ItemCell类中实现didSelect UICollectionView委托方法

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
   productVC.performSegue(withIdentifier: "pass your segue identifier here..", sender: nil)
}

在viewcontroller中实现
ProductCellDelegate
。调用performsgue进行导航。

使用委托模式告知tableViewController执行segue您只能从一个ViewController执行segue(到另一个)。这就是为什么你不能在“牢房里”做这件事。您已经使用了委托:
UITableView
UICollectionView
。同样,在您的
CollectionCell
中添加一个自定义委托,以便在选择collectionviewcell时通知您的ViewController。这正是我所需要的!