[英]<Swift>How to navigate to a new page in custom cell?
I have a custom cell FeedCell.swift and the controller FeedViewController.swift.我有一个自定义单元格 FeedCell.swift 和控制器 FeedViewController.swift。 And I set a btn in each cell so that I can navigate to detailController.swift.我在每个单元格中设置了一个 btn,以便我可以导航到 detailController.swift。 But in custom cell I can't use self.navigation.但是在自定义单元格中我不能使用 self.navigation。 What should I do?我该怎么办?
You can set an outlet of Button in cell and addTarget it like :您可以在单元格中设置 Button 的出口并像这样添加目标:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! yourCell
cell.yourButton.addTarget(self, action: #selector(goDetail), for: .touchUpInside)
return cell
}
@objc func goDetail(){
let vc = // Declare your viewController
self.navigationController?.pushViewController(vc, animated:true)
}
Btw you can add this action when a cell selected instead of using button顺便说一句,您可以在选择单元格而不是使用按钮时添加此操作
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let vc = // Declare your viewController
self.navigationController?.pushViewController(vc, animated:true)
}
If you want to send which cell's button clicked just add cell.yourButton.tag = indexPath.row
in cellForRowAt
and modify goDetail
with如果你想发送点击哪个单元格的按钮,只需在cellForRowAt
添加cell.yourButton.tag = indexPath.row
并修改goDetail
@objc func goDetail(sender : UIButton){
sender.tag // while give your selected cell index
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.