UITableView重复单元格(带有文本字段的自定义单元格)

编程入门 行业动态 更新时间:2024-10-03 17:17:02
本文介绍了UITableView重复单元格(带有文本字段的自定义单元格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我花了几天的时间解决这个问题,在尝试了很多之后,我在这里提出了一个问题.我正在使用自定义UITableViewCell,并且该单元格包含UITextFields.在将新单元格添加到表格视图时,表格视图表现出异常,就像它复制了该单元格一样,当我尝试编辑新单元格的文本字段时,先前cel的文本字段也会被编辑.

I have spent days on resolving this issue and after trying much I am asking a question here. I am using a custom UITableViewCell and that cell contains UITextFields. On adding new cells to the table view, the table view behaves abnormal like it duplicates the cell and when I try to edit the textfield of new cell, the textfield of previous cel gets edited too.

复制行为如下:第一个单元与第三个单元重复.我不知道这是由于电池的可重用性引起的,但是有人能告诉我有效的解决方案吗?

The behavior of duplication is as follows: 1st cell is duplicated for 3rd cell. I don't know this is due to reusability of cells but could anyone tell me about the efficient solution?

我附上了UITableViewCell的屏幕截图.

I am attaching the screenshot of UITableViewCell.

cellForRow的代码如下:

The code for cellForRow is as follows:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell : Product_PriceTableViewCell = tableView.dequeueReusableCell(withIdentifier: "product_priceCell") as! Product_PriceTableViewCell cell.dropDownViewProducts.index = indexPath.row cell.txtDescription.index = indexPath.row cell.tfPrice.index = indexPath.row cell.dropDownQty.index = indexPath.row cell.tfTotalPrice_Euro.index = indexPath.row cell.tfTotalPrice_IDR.index = indexPath.row cell.dropDownViewTotalDiscount.index = indexPath.row cell.dropDownViewDeposit.index = indexPath.row cell.tfTotalDeposit_Euro.index = indexPath.row cell.tfRemaingAfterDeposit_IDR.index = indexPath.row return cell }

推荐答案

问题是UITableView正在重用单元格,这是您希望实现良好滚动性能的原因.

The issue is the cell is being reused by the UITableView, which is what you want to happen for good scrolling performance.

您应该更新支持表中每一行的数据源,以保存用户在字段中输入的文本.

You should update the data source that supports each row in the table to hold the text the user inputs in the field.

然后在cellForRowAt中从您的数据源分配文本字段的text属性.

Then have the text field's text property assigned from your data source in cellForRowAt.

换句话说,每次在屏幕上看到UITableViewCell时,UITableViewCell都是相同的 instance ,UITextField也是如此,因此它的text属性也是如此.这意味着每次调用cellForRowAt时都需要为其分配正确的文本值.

In other words, the UITableViewCell is the same instance each time you see it on the screen, and so is the UITextField and therefore so is it's text property. Which means it needs to be assigned it's correct text value each time cellForRowAt is called.

我不确定您的代码,因此我提供了一个示例,说明如何执行您想要的操作:

I'm unsure of your code so I have provided an example of how I would do something like what you want:

class MyCell: UITableViewCell { @IBOutlet weak var inputField: UITextField! } class ViewController: UIViewController { @IBOutlet weak var table: UITableView! var items = [String]() fileprivate func setupItems() { items = ["Duck", "Cow", "Deer", "Potato" ] } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. setupItems() } } extension ViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // the # of rows will equal the # of items return items.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // we use the cell's indexPath.row to // to get the item in the array's text // and use it as the cell's input field text guard let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as? MyCell else { return UITableViewCell() } // now even if the cell is the same instance // it's field's text is assigned each time cell.inputField.text = items[indexPath.row] // Use the tag on UITextField // to track the indexPath.row that // it's current being presented for cell.inputField.tag = indexPath.row // become the field's delegate cell.inputField.delegate = self return cell } } extension ViewController: UITextFieldDelegate { // or whatever method(s) matches the app's // input style for this view func textFieldDidEndEditing(_ textField: UITextField) { guard let text = textField.text else { return // nothing to update } // use the field's tag // to update the correct element items[textField.tag] = text } }

更多推荐

UITableView重复单元格(带有文本字段的自定义单元格)

本文发布于:2023-11-26 09:56:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   自定义   字段   文本   UITableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!