-4

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

Bhavesh Nayi
  • 705
  • 4
  • 15
B K.
  • 534
  • 5
  • 18

4 Answers4

5

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)    
}
mahdi
  • 439
  • 3
  • 15
1

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.

Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • Please share the code so i will get better idea – B K. May 08 '19 at 11:34
  • 1
    @BhavishaKhatri no ios developer doesn't know `didSelectRowAt` your question is miss understood that you need a navigation from a specific action inside the cell , Do you still find it impossible to see duplicates and read other answers here with `didSelectRowAt` – Shehata Gamal May 08 '19 at 11:39
0

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.

Nitish
  • 13,845
  • 28
  • 135
  • 263
0

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)
 }
steveSarsawa
  • 1,559
  • 2
  • 14
  • 31