This question already has an answer here:
-
navigate on click of collectionview cell inside tableview
4 answers
-
Programmatically navigate to another view controller/scene
17 answers
-
Is there a way to navigate from a tableViewCell to another view controller when a cell is swiped?
2 answers
Using UITableviewcell
class for displaying the cell of table list. I want to redirect from one screen to another using "navigationController?.pushViewController
", but it is not supporting in table cell class. So how can i move UIViewcontroller
using pushViewController from table cell.
My code is:
class TableCell: UITableViewCell
{
let storyBoard : UIStoryboard = UIStoryboard(name: "SubMain", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "DetailReport") as! DetailReport
nextViewController.aryOfResultList = aryForCreateJSONResult
cell.inputViewController?.navigationController?.pushViewController(nextViewController, animated: true)
}
But its not working. Please give me the solution of my question
You can always navigate using the navigation controller from one view controller to another and table view cell is not a UIViewController
type, its a UIView
type.
You can either create a delegate in custom table view cell and that will confirm the UIViewController
and implement the cell delegate's method in view controller.
or you can navigate to other screen then cell is tapped in table view.
Get the ViewController from helper function :
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
and then in your custom cell, use parentViewController.navigationController
You can use this for all UIView
types.
Declare a variable in the cell called var parent: UIViewController?
.
in the parent controller, in the function cellForRow
,
cell.parent = self
now in the cell you can call
parent?.navigationController.pushViewController
Update
if what u want is simply to push the view controller when a cell is taped, u can do it in the parent controller in:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.navigationController?.pushViewController(nextViewController, animated: true)
}
You've to assign pushViewController
code into tableView didSelectRowAt()
method
e.g.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let storyBoard : UIStoryboard = UIStoryboard(name: "SubMain", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "DetailReport") as! DetailReport
navigationController?.pushViewController(nextViewController, animated: true)
}