swift - 如何导航到自定义单元格中的新页面?
问题描述
我有一个自定义单元 FeedCell.swift 和控制器 FeedViewController.swift。我在每个单元格中设置了一个 btn,以便我可以导航到 detailController.swift。但是在自定义单元格中我不能使用 self.navigation。我应该怎么办?
解决方案
您可以在单元格中设置按钮的出口并添加目标,如下所示:
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)
}
顺便说一句,您可以在选择单元格而不是使用按钮时添加此操作
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let vc = // Declare your viewController
self.navigationController?.pushViewController(vc, animated:true)
}
如果您想发送单击了哪个单元格的按钮,只需添加cell.yourButton.tag = indexPath.row
并cellForRowAt
修改goDetail
@objc func goDetail(sender : UIButton){
sender.tag // while give your selected cell index
}
推荐阅读
- javascript - 将getJSON的结果分成两个部分,并使用每个部分显示不同的DIV
- python - 在 Chrome 中仅关闭 1 个选项卡
- android - 如何在不同的活动中将文本设置为 TextView
- java - 如何在 macOS 上将路径参数传递给 java
- javascript - JavaScript:运算符优先级
- amazon-cloudwatch - 使用字段点键过滤 CloudWatch 中的 JSON 日志事件
- java - Dagger 2错误在android中注入依赖项
- javascript - Fetch API 方法中的 React Native Catch 不起作用
- c# - 在 C# 控制台应用程序中使用进度条
- c++ - 为什么模板化的非 const 参数构造函数优于给定的复制构造函数