ios - 如何根据点击的单元格更改视图控制器的数据
问题描述
我正在做一个作业,在 ViewController A 中,我有三个单元格,钙、碱度、镁。我需要根据在 ViewController A 中点击的单元格更新 ViewController B。即,如果点击了碱度,则 ViewController B 上的标签应该说“碱度”,如果点击了钙,则 ViewController 2 应该说钙等。
我应该为每个单元格创建一个转场吗?或者我可以通过代码做到这一点吗?
我尝试使用 segue,但我觉得它对很多 segues 来说都是如此……我正在考虑使用单元标签?
还没有错误
解决方案
没有segues的选项:
1) 在 interfaz builder 中,为ViewControllerB
2)ViewControllerA
需要符合UITableViewDelegate
协议(或者UICollectionViewDelegate
如果你使用UICollectionView
)
3)实现tableView(_:didSelectRowAt:)
方法ViewControllerA
4)在方法中,实例化一个ViewControllerB
实例,例子:
let vc = UIStoryboard(name: "nameofthestoryboard").instantiateViewController(withIdentifier: "ViewControllerB") as! ViewControllerB
5)获取使用索引路径选择的单元格。
let cell = tableview.cellForRow(at: indexPath) as! YourCellType
6) 为viewControllerB设置必要的信息,例如:
vc.chemicalElement = cell.chemicalElement
7) 显示您创建的 ViewControllerB 实例。
present(vc, animated: true, completion: nil)
仅此而已。
推荐阅读
- r - R Highcharter - 在多个堆积柱形图中突出显示同一组+列中的顺序组
- reactjs - react-select - 如何确保默认选择的项目没有出现在两个下拉列表中?
- node.js - 在 MongoDB 中创建管理员角色并登录
- php - 确定发电机是打开还是关闭
- java - 如何知道操作系统杀死我的前台服务android
- spring-boot - 在spring data jpa中通过@Query更新Entity内部的关联对象
- mysql - 根据订购日期获取每个客户的最后三个最新订单金额
- django - Django REST框架无法识别
- css - 如何根据屏幕宽度更改 NextJS Image 标签的高度?
- flutter - 是否可以在不使用 Scaffold 的 Body 的情况下使用 BottomNavigatorBar