我有带文本字段的tableview单元格,但是当我添加新行并向上或向下滚动tableview时,已删除的文本字段值被删除。我在2015年对以前的问题做了很多研究,但未正确回答。如何解决此问题?如果可以,如何使用文本字段委托方法?这是我的代码:
var i = 0
while i <= taskArrForRead.count {
let indexPath = IndexPath(row: i, section: 0)
let cell = self.tableView.cellForRow(at: indexPath) as? taslakDuzenlemeCell
if let item = cell?.taslakTextField.text! {
arrayOfNames.append(item)
}
i = i + 1
}
在此代码中,我从表格视图中获取所有文本字段值,但是如果表格视图滚动消失,则值将变为默认值。我该如何解决?谢谢。
这是我的tableview代码:
extension taslakOlusturmaController: UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return taskArrForRead.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "taslakCell", for: indexPath) as! taslakDuzenlemeCell
cell.taslakTextField.text = taskArrForRead[indexPath.item]
cell.taslakTextField.delegate = self
return cell
}
func textFieldDidEndEditing(_ textField: UITextField) {
print(textField.text!)
self.arrayOfNames.append(textField.text!)
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "taslakCell", for: indexPath) as! taslakDuzenlemeCell
cell.taslakTextField.text = taskArrForRead[indexPath.item]
print(arrayOfNames[indexPath.item])
}
}
使用textfield delegate
方法可以获取已删除的文本字段值,但无法再次将其带到文本字段。用户再次滚动后看不到值,我也可以使用didEndDisplaying cell
方法删除值。
答案 0 :(得分:0)
您可以使用方法PrepareForReuse
重新设置单元格时重新设置文本。