从 collectionview 内的 Tableview 单元格导航到视图控制器 [英] Navigate to view controller from Tableview cell inside collectionview

查看:26
本文介绍了从 collectionview 内的 Tableview 单元格导航到视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have tableview embedded in collectionview. I have xib for tableview. When user select a cell of tableview I want to navigate to another view controller.

我试过这个方法,但它不起作用

I tried this method but its not working

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

但它不起作用,因为此视图控制器未添加到导航堆栈中.

But its not working because this viewcontroller in not added to navigation stack.

    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)


    }

}

我该如何解决这个问题.任何帮助表示赞赏.

How do i solve this problem. Any help is appreciated.

推荐答案

您有以下选择

1) 在viewController 中实现tableview 数据源和delgate 而不是collection view cell
2)使用委托(下面解释)
3) 使用闭包
4) 使用通知中心

1) Implement tableview datasource and delgate in viewController instead of collection view cell
2) Use Delegate (explained below )
3) Use Closures
4) Use NotificationCenter

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

You need to create delegate or protocol as collection view cell can't push or present view controller.

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

Here is simple example (This is not exact code you may need modification)

创建协议

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

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

Inside your collectionview cell add weak property

weak var delegate:TableViewInsideCollectionViewDelegate?

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

Now in your ViewController class you in cellForItem method of collectionview you need to set delegate to self

喜欢

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

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

and implement delegate method in viewController class and write code to push your view controller from there like self.navigationController.push

现在在 Goto Collectionview Cell 方法中

Now In Goto Collectionview Cell method

并且每当您的 tableviewDidSelect 调用

and whenever your tableviewDidSelect called

调用委托方法,如self.delegate?.cellTaped(data: dataYouWantToPass)

希望对你有帮助

这篇关于从 collectionview 内的 Tableview 单元格导航到视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆