简体   繁体   English

Swift-原型单元从TextField获得价值

[英]Swift - Prototype Cell get value from TextField

I have a tableView with 2 prototype Cells. 我有一个带有2个原型单元格的tableView。

The first Prototype Cell has a TextField and Button on the right. 第一个原型单元格的右侧有一个TextField和Button。 Let's say it "Cell1" 假设它是“ Cell1”

The second Prototype Cell has only a label. 第二个原型单元格只有一个标签。 Let's say it "Cell2" 假设它是“ Cell2”

I just want to type something in the first cell and then by taping the Button to : 我只想在第一个单元格中键入一些内容,然后点击Button到:

1) Create a new Cell2 Prototype Cell Row 1)创建一个新的Cell2原型单元行

2) That new Cell2 Prototype Cell Row must have a Label with the value i entered on the Cell1 TextField. 2)该新的Cell2原型单元行必须具有一个在Cell1 TextField上输入值i的标签。

So with that way i could have a list of Cell2 Rows with different values of TextField. 因此,通过这种方式,我可以得到一个具有不同TextField值的Cell2行的列表。

I know how to achieve the first (the new Row created) but not the second. 我知道如何实现第一个(创建的新行),而不是第二个。

When the segue starts, i have only the Cell1 Prototype like this photo : 当segue开始时,我只有Cell1原型,如这张照片: 从这里开始

And when i tap the + Button, i have a new Row of "Cell2" Prototype. 当我点击+按钮时,我有了一个新的“ Cell2”原型行。 This kind of Cell must have the label of the Textfield everytime. 这种单元必须每次都具有Textfield的标签。 在此处输入图片说明

How can i take the value of TextField everytime and pass it to new created Cell2 Row Label? 如何每次都获取TextField的值并将其传递给新创建的Cell2行标签?

In all probability your difficulty is merely that your data model is a little too simple-minded. 您的困难很可能只是您的数据模型过于简单。 Keep in mind that all the work must be done by your cellForRowAtIndexPath: implementation consulting the data model. 请记住,所有工作必须由您的cellForRowAtIndexPath:实现咨询数据模型来完成。 Thus the data model must know two things for each row: 因此,数据模型必须为每一行知道两件事:

  1. Is this a prototype 1 row or a prototype 2 row? 这是原型1行还是原型2行?

  2. What is the text field (if this is a prototype 1 row) or label (if this is a prototype 2 row) value for this row? 该行的文本字段(如果是原型1行)或标签(如果是原型2行)的值是多少?

Given a data structure that carries that information, it is clear enough how to proceed: 给定一个携带该信息的数据结构,那么足够清楚如何进行:

  • As the user types in a text field, the text field delegate should enter the text field's text into the model at that row. 当用户键入文本字段时,文本字段委托应在该行的模型中输入文本字段的文本。

  • When the user hits the Plus button, we figure out what row this is and insert into the data model, immediately after the current row, a new entry marked as a prototype 2 row with the same text value as the current row. 当用户单击加号按钮时,我们将找出这是哪一行,并在当前行之后立即将一个新条目插入到数据模型中,该条目标记为原型2行,其文本值与当前行相同。 We then insert the row into the table as well, so that our cellForRowAtIndexPath: is called for the new row. 然后,我们cellForRowAtIndexPath:行插入到表中,以便为新行调用cellForRowAtIndexPath:

Given those simple instructions, it's quite easy to build a working example of the sort of thing you're trying to do: 有了这些简单的说明,就可以轻松地构建一个您要尝试执行的操作示例:

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM