如何在 TableCell 类中使用 navigationController.pushViewController? [英] How to use navigationController.pushViewController in TableCell class?
问题描述
使用UITableviewcell
类来显示表格列表的单元格.我想使用navigationController?.pushViewController
"从一个屏幕重定向到另一个屏幕,但它在表格单元格类中不受支持.那么如何使用表格单元格中的 pushViewController 移动 UIViewcontroller
.
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.
我的代码是:
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
推荐答案
在单元格中声明一个名为 var parent: UIViewController?
的变量.在父控制器中,在函数 cellForRow
,
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)
}
这篇关于如何在 TableCell 类中使用 navigationController.pushViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!