How to use navigationController.pushViewController

2019-09-30 02:33发布

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

4条回答
姐就是有狂的资本
2楼-- · 2019-09-30 02:50

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)    
}
查看更多
聊天终结者
3楼-- · 2019-09-30 02:57

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.

查看更多
乱世女痞
4楼-- · 2019-09-30 02:57

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)
 }
查看更多
手持菜刀,她持情操
5楼-- · 2019-09-30 03:08

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.

查看更多
登录 后发表回答