如何修复表格视图单元格表单上的重复数据?
How to fix repeating data on tableview cell form?
这个答案已经搜索了大约一个月,但找不到解决方案。
如何修复重复的单元格数据,使其在滚动时不重复?我输入数据的唯一字段是“3731 Black Cherry Greek”当我滚动时它会复制案例和单位字段。 “3665 Coconut NOUNOS”和“1243 Rasp Lemonade SS”应该仍然是空白的。我花了几个小时试图找到解决方案。任何帮助将不胜感激。谢谢
Image
func numberOfSections(in tableView: UITableView) -> Int { return 1 }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return inventoryArray.count }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! cell
cell.unitField.tag = (indexPath as NSIndexPath).row
cell.unitField.addTarget(self, action: #selector(Inventory.unitCell(_:)), for: UIControl.Event.editingDidEnd)
return cell as cell
}
@IBAction func unitCell(_ sender: UIButton) {
let tag = sender.tag
let cell = tableView.cellForRow(at: IndexPath.init(row: tag, section: 0)) as! InventoryCell
var intUnit = Int(cell.unitField.text!)!
cell.count.text = String (intUnit)
}}
请务必记住,您实际上是在重复使用细胞。如果您修改其中一个并向下滚动,相同的单元格将再次出现,这就是您遇到的情况。
为了解决这个问题,您需要准确定义单元格每次重新出现时的外观。如果有数据,请将其添加到标签中。否则,将标签重置为空白。
cell.unitField.tag = (indexPath as NSIndexPath).row
cell.unitField.text = "" // empty the textField
cell.unitField.addTarget(self, action: #selector(Inventory.unitCell(_:)), for: UIControl.Event.editingDidEnd)
如果您不手动重置它,它会保留您上次给它的信息。
这个答案已经搜索了大约一个月,但找不到解决方案。
如何修复重复的单元格数据,使其在滚动时不重复?我输入数据的唯一字段是“3731 Black Cherry Greek”当我滚动时它会复制案例和单位字段。 “3665 Coconut NOUNOS”和“1243 Rasp Lemonade SS”应该仍然是空白的。我花了几个小时试图找到解决方案。任何帮助将不胜感激。谢谢
Image
func numberOfSections(in tableView: UITableView) -> Int { return 1 }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return inventoryArray.count }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! cell
cell.unitField.tag = (indexPath as NSIndexPath).row
cell.unitField.addTarget(self, action: #selector(Inventory.unitCell(_:)), for: UIControl.Event.editingDidEnd)
return cell as cell
}
@IBAction func unitCell(_ sender: UIButton) {
let tag = sender.tag
let cell = tableView.cellForRow(at: IndexPath.init(row: tag, section: 0)) as! InventoryCell
var intUnit = Int(cell.unitField.text!)!
cell.count.text = String (intUnit)
}}
请务必记住,您实际上是在重复使用细胞。如果您修改其中一个并向下滚动,相同的单元格将再次出现,这就是您遇到的情况。
为了解决这个问题,您需要准确定义单元格每次重新出现时的外观。如果有数据,请将其添加到标签中。否则,将标签重置为空白。
cell.unitField.tag = (indexPath as NSIndexPath).row
cell.unitField.text = "" // empty the textField
cell.unitField.addTarget(self, action: #selector(Inventory.unitCell(_:)), for: UIControl.Event.editingDidEnd)
如果您不手动重置它,它会保留您上次给它的信息。