简体   繁体   English

如何基于所点击的单元格在视图控制器中更改数据

[英]How to change data one a view controller based on the cell tapped

I am doing an assignment, In ViewController A, I have three cells, Calcium, Alkalinity, Magnesium. 我正在做一个分配,在ViewController A中,我有三个单元格:钙,碱度,镁。 I need to update ViewController B based on the cell tapped in ViewController A. ie if Alkalinity was tapped, the label on ViewController B should say "Alkalinity", if calcium is tapped, ViewController 2 should say calcium etc. 我需要基于在ViewController A中点击的单元格来更新ViewControllerB。即,如果点击了碱度,则ViewController B上的标签应显示为“碱度”,如果点击了钙,则ViewController 2应显示钙等。

Should I create a segue for each cell? 我应该为每个单元创建一个序列吗? Or can I do this via code? 或者我可以通过代码做到这一点?

I tried using a segue but I feel as if it is to many segues... Im thinking to use cell tags? 我尝试使用segue,但感觉好像对许多segue一样...我想使用细胞标记吗?

no error yet 尚无错误

Option without segues: 没有选择的选项:

1) In interfaz builder, assign a identifier to ViewControllerB 1)在interfaz构建器中,为ViewControllerB分配一个标识符

2) ViewControllerA need conform UITableViewDelegate protocol (or UICollectionViewDelegate if you use UICollectionView ) 2) ViewControllerA需要符合UITableViewDelegate协议(如果使用UICollectionView则需要UICollectionViewDelegate协议)

3) Implement the method tableView(_:didSelectRowAt:) in ViewControllerA 3)在ViewControllerA实现方法tableView(_:didSelectRowAt:)

4) In the method, instantiate a ViewControllerB instance, example: 4)在方法中,实例化一个ViewControllerB实例,例如:

let vc = UIStoryboard(name: "nameofthestoryboard").instantiateViewController(withIdentifier: "ViewControllerB") as! ViewControllerB

5) Get the cell selected with the index path. 5)获取带有索引路径的选定单元格。

let cell = tableview.cellForRow(at: indexPath) as! YourCellType

6) Setup the necessary information to viewControllerB, example: 6)设置必要的信息到viewControllerB,例如:

vc.chemicalElement = cell.chemicalElement

7) Present the ViewControllerB instance that you created. 7)显示您创建的ViewControllerB实例。

present(vc, animated: true, completion: nil) 

And that is all. 仅此而已。

If you are using a simple segue to move between view controllers than to pass data from one to the other you can do something like this. 如果您使用一种简单的方法在视图控制器之间移动,而不是将数据从一个传递到另一个,则可以执行以下操作。

You need to instantiate the title variable in ViewControllerA. 您需要在ViewControllerA中实例化title变量。

var elementName : String?   

Using the function prepare(for segue: UIStoryboardSegue, sender: Any?) 使用函数prepare(for segue: UIStoryboardSegue, sender: Any?)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "SEGUE IDENTIFIER HERE") {
       var vc = segue.destinationViewController as ViewControllerB 
       vc.title = elementName
    }      
}

You also need to instantiate the title variable in ViewControllerB. 您还需要在ViewControllerB中实例化title变量。

var title : String? 

If you post your code I can give you more specific help otherwise this is the flow you could use to accomplish this task 如果您发布代码,我可以为您提供更多具体的帮助,否则这就是您可以用来完成此任务的流程

You can create a public variable on viewController B, 您可以在viewController B上创建一个公共变量,

public var componentName : String?

then pass the selected value(from did select method) from ViewController A. 然后从ViewController A传递选择的值(来自select方法)。

let ViewB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewcontrollerB")
ViewB.componentName = componentName //get this component name from index path
self.navigationController?.show(ViewB, sender: nil)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在一个视图控制器上有两个 TableView,如何根据点击的 TableView 将数据传递给单独的视图控制器? - I have two TableViews on a single View Controller, how can I pass data to a separate View Controller based upon which TableView was tapped? 如何根据点击的UITableCell更改目标视图控制器的内容? - How to change content of the destination view controller according to tapped UITableCell? 当我轻按任何集合视图单元格时(不点击最后一个单元格),如何移动另一个视图控制器? - How to move another view controller when i tapped any collection view cell (without taping last cell)? 在被点击的单元格的索引处更改数据源 - Change the data source at the index of the cell that was tapped 基于先前表视图中点击的单元格的快速动态视图 - Swift dynamic view based on cell tapped in previous table view 如何更改重复单元中点击的按钮图像? - How to change button image on tapped in repeating cell? 当我们点击它时,如何更改添加在表格视图单元格上的按钮背景色? - How to change button background color which is added on table view cell when we tapped on it? 轻触单元格时,表格视图单元格会更改高度 - Table View Cells change Height when cell tapped 允许一次点击和更新集合视图单元格中的一个按钮 - Allow one button in collection view cell to be tapped and updated at a time Swift:在点击一个单元格时更改所有UITableViewCell的文本颜色 - Swift: Change text color of all UITableViewCells when one cell is tapped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM