问题
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
回答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.
回答2:
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:
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:
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)
}
来源:https://stackoverflow.com/questions/56039560/how-to-use-navigationcontroller-pushviewcontroller-in-tablecell-class