在表视图单元格中编辑文本字段的最佳方法

时间:2022-01-01 22:19:56

In my app I've got a number of views that need to be in-place editable. I've got the tableviewcells setup to include a UITextField which is enabled, and the text can be changed and this is fine, it works.

在我的应用中,我有很多视图需要就地可编辑。我已经设置了tableviewcells来包含一个UITextField,它是启用的,文本可以修改,这很好,它可以工作。

My question is what is the best approach to keeping track of these? Should I :

我的问题是,跟踪这些信息的最好方法是什么?我应该:

  • Keep an iVar of each textfield. If so then
    • How do I make sure each cell has the correct field if it's only instantiated once?
    • 如果每个单元格只有一次实例化,如何确保每个单元格都有正确的字段?
  • 保持每个文本字段的iVar。如果是,那么如果只实例化一次,如何确保每个单元格都有正确的字段?
  • Have a textfield added when the cell is created and simply set its value when configuring the cell. If so then
    • How do I keep track of the values? If the cell went out of view and then back, its values would be reset to its original value and not the edited value.
    • 如何跟踪这些值?如果单元格离开视图然后返回,那么它的值将重置为原始值,而不是编辑值。
  • 在创建单元格时添加一个textfield,并在配置单元格时简单地设置它的值。如果是,那么我如何跟踪这些值呢?如果单元格离开视图然后返回,那么它的值将重置为原始值,而不是编辑值。

What approach do you generally tend to use for this scenario? Especially if there are a large number of cells that you're dealing with.

在这种情况下,您通常倾向于使用什么方法?特别是当你要处理大量的单元格时。

3 个解决方案

#1


2  

The best way is to follow MVC and implement your model, which in your case is the textfield values. What you do is store those values in an array, and in your cellForIndexPath method you reference the correct value in the array. For example, the first row of the table (indexPath.row == 0) would retrieve the first element in the array. Changes in the textField would be recorded on the corresponding element in the array.

最好的方法是遵循MVC并实现您的模型,在您的例子中是textfield值。您要做的是将这些值存储在数组中,并在cellForIndexPath方法中引用数组中的正确值。例如,表的第一行(indexPath)。row == 0)将检索数组中的第一个元素。textField中的更改将记录在数组中相应的元素上。

#2


10  

  1. Store the values for all of your text fields in a mutable array in your data model.

    在数据模型的可变数组中存储所有文本字段的值。

  2. In tableView:willDisplayCell:forRowAtIndexPath: set your text field's value, using the value from your array based on the index path.

    在tableView中:willDisplayCell:forRowAtIndexPath:使用基于索引路径的数组中的值设置文本字段的值。

  3. When a text field ends editing, store the value back in the array immediately. There is already a protocol for this (so no need to write your own), but it is a little trickier than normal because you are not handed the indexPath and need to find the one associated with your text field:

    当文本字段结束编辑时,立即将值存储回数组中。这里已经有了一个协议(所以不需要自己编写),但是它比普通的要复杂一些,因为您没有获得indexPath,需要找到与文本字段相关联的那个:

    - (void) textFieldDidEndEditing:(UITextField *)textField {
        CGRect location = [self convertRect:textField.frame toView:self.tableView];
        NSIndexPath *indexPath = [[self.tableView indexPathsForRowsInRect:location] objectAtIndex:0];
    
        // Save the contents of the text field into your array:
        [yourArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
    }
    

#3


2  

  • Subclass UITableViewCell and add a UITextField in it's init method.

    子类UITableViewCell并在它的init方法中添加一个UITextField。

  • create a protocol for the custom cell class. when UITextField begin editing or end editing call the delegate's method.

    为自定义单元类创建协议。当UITextField开始编辑或结束编辑时,调用委托的方法。

  • in controller, implement the custom cell's protocol and set the indexpath row value to cell's tag in tableView:cellForRowAtIndexPath:

    在controller中,实现自定义单元格协议,并在tableView:cellForRowAtIndexPath:

so every time you edit UITextField, you can know in controller which row is editing (via cell's tag)

每次你编辑UITextField时,你可以在控制器中知道哪个行正在编辑(通过单元格的标签)

#1


2  

The best way is to follow MVC and implement your model, which in your case is the textfield values. What you do is store those values in an array, and in your cellForIndexPath method you reference the correct value in the array. For example, the first row of the table (indexPath.row == 0) would retrieve the first element in the array. Changes in the textField would be recorded on the corresponding element in the array.

最好的方法是遵循MVC并实现您的模型,在您的例子中是textfield值。您要做的是将这些值存储在数组中,并在cellForIndexPath方法中引用数组中的正确值。例如,表的第一行(indexPath)。row == 0)将检索数组中的第一个元素。textField中的更改将记录在数组中相应的元素上。

#2


10  

  1. Store the values for all of your text fields in a mutable array in your data model.

    在数据模型的可变数组中存储所有文本字段的值。

  2. In tableView:willDisplayCell:forRowAtIndexPath: set your text field's value, using the value from your array based on the index path.

    在tableView中:willDisplayCell:forRowAtIndexPath:使用基于索引路径的数组中的值设置文本字段的值。

  3. When a text field ends editing, store the value back in the array immediately. There is already a protocol for this (so no need to write your own), but it is a little trickier than normal because you are not handed the indexPath and need to find the one associated with your text field:

    当文本字段结束编辑时,立即将值存储回数组中。这里已经有了一个协议(所以不需要自己编写),但是它比普通的要复杂一些,因为您没有获得indexPath,需要找到与文本字段相关联的那个:

    - (void) textFieldDidEndEditing:(UITextField *)textField {
        CGRect location = [self convertRect:textField.frame toView:self.tableView];
        NSIndexPath *indexPath = [[self.tableView indexPathsForRowsInRect:location] objectAtIndex:0];
    
        // Save the contents of the text field into your array:
        [yourArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
    }
    

#3


2  

  • Subclass UITableViewCell and add a UITextField in it's init method.

    子类UITableViewCell并在它的init方法中添加一个UITextField。

  • create a protocol for the custom cell class. when UITextField begin editing or end editing call the delegate's method.

    为自定义单元类创建协议。当UITextField开始编辑或结束编辑时,调用委托的方法。

  • in controller, implement the custom cell's protocol and set the indexpath row value to cell's tag in tableView:cellForRowAtIndexPath:

    在controller中,实现自定义单元格协议,并在tableView:cellForRowAtIndexPath:

so every time you edit UITextField, you can know in controller which row is editing (via cell's tag)

每次你编辑UITextField时,你可以在控制器中知道哪个行正在编辑(通过单元格的标签)