从 collectionview 内的 Tableview 单元导航到视图控制器

Navigate to view controller from Tableview cell inside collectionview

所以我在 collectionview 中嵌入了 tableview。 我有用于 tableview 的 xib。 当用户 select tableview 的一个单元格时,我想导航到另一个视图控制器。

我试过这个方法,但没用

let storyboardId = "Login"
        let vc = storyboard?.instantiateViewController(withIdentifier: storyboardId) 
        navigationController?.pushViewController(vc!, animated: true)

但它不起作用,因为此 viewcontroller 未添加到导航堆栈。

    class DetailCollectionViewCell: UICollectionViewCell, UITableViewDelegate,UITableViewDataSource {


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

//        let navigationController = UINavigationController()
//        let storyboardId = "Login"
//        let vc = storyboard?.instantiateViewController(withIdentifier: storyboardId)
//        navigationController?.pushViewController(vc!, animated: true)


    }

}

我该如何解决这个问题。 感谢任何帮助。

您有以下选项

1) 在 viewController 中实现表视图数据源和委托而不是集合视图单元格
2) 使用委托(下面解释)
3) 使用闭包
4) 使用通知中心

您需要创建委托或协议,因为集合视图单元无法推送或呈现视图控制器。

这是一个简单的例子(这不是您可能需要修改的确切代码)

创建协议

protocol TableViewInsideCollectionViewDelegate:class {
    func cellTaped(data:IndexPath)
}

在您的 collectionview 单元格中添加 weak 属性

weak var delegate:TableViewInsideCollectionViewDelegate?

现在在你的 ViewController class 你在 collectionview 的 cellForItem 方法中 您需要将委托设置为 self

喜欢

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCell", for: indexPath) as! CustomCollectionCell
    cell.delegate = self
    return cell

并在 viewController class 中实现委托方法并编写代码以从那里推送视图控制器,例如 self.navigationController.push

现在在 Goto Collectionview Cell 方法中

并且每当您的 tableviewDidSelect 调用

self.delegate?.cellTaped(data: dataYouWantToPass)

一样调用委托方法

希望对您有所帮助

您必须检查一些信息:

  • 首先: 检查您的 navigationController 是否为零
  • 第二: 检查您的初始视图控制器方法是否正确, 这是我的方式:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    让viewController = storyboard.instantiateViewController(withIdentifier: "StoryboardIdentifier") 作为? ViewController

您可以使用委托模式解决此问题,步骤如下:

  • 确认table 视图委托给集合视图,集合视图委托给各自的视图控制器。
  • 委托链可以用来解决这个问题。在此示例中,我展示了如何将数据从 table 视图单元格传递到集合视图单元格。
  • 实现集合视图委托和数据源方法。
  • 实现 table 查看委托和数据源方法。
  • 无论什么时候 select 行都会调用调用委托方法来告诉视图控制器某些行已 selected 并根据行索引更改处理您的导航。

代码示例:

  • 第 1 步:创建协议。

    protocol RowSelected : class {
     func rowSelected(_ index : Int)
    }
    
  • 第 2 步:在 TableViewCell 中声明委托变量。

    weak var delegate: RowSelected?
    
  • 第 3 步:在集合视图中确认委托并实施委托方法。

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath) as! CollectionViewCell
      cell.delegate = self
      return cell
    
      extension CollectionViewCell : RowSelected {
         func rowSelected() {
          // Pass the information to view controller via a delegate/closure/notification. just like we passed information from table view cell to collection view cell and handle navigation accordingly.
         }
      }
    
  • 第 4 步:在 ViewController 中,您可以确认集合视图的委托并实现它的委托方法并可以处理导航。

您也可以使用闭包和通知中心来通知视图控制器导航到下一个屏幕。