【发布时间】:2017-12-24 00:22:37
【问题描述】:
我正在使用字典存储cellForRowAt: 上表格视图中的单元格,然后当单元格中的 textField 完成编辑时,我正在更新字典的内容以反映新单元格。这样当单元格滚动出屏幕时,再返回输入的值仍会显示。
当再次调用cellForRowAt: 时,我从字典中检索单元格并返回它,但是返回的单元格的 textField 不包含任何信息。
我已经通过使用与放入字典时相同的方法从字典中检索单元格来测试这一点,并且 textField 文本仍然存在。
这是在 textField 更改后替换单元格的代码:
func textFieldDidEndEditing(_ textField: UITextField) {
if textField.tag == 0 {
print("Replacing value in tableCells")
let tempCell = tableCells["\(textField.tag)"] as! profileNameCell
tempCell.profileName.text = textField.text
tableCells["\(textField.tag)"] = tempCell
} else {
let tempCell = tableCells["\(textField.tag)"] as! profileInfoCell
tempCell.infoField.text = textField.text
tableCells["\(textField.tag)"] = tempCell
}
}
这是cellForRowAt:中的代码:
let indexString = "\(indexPath.row)"
if tableCells[indexString] as! UITableViewCell == cellFiller {
print("Replacing tableCells[\(indexString)] with cell")
tableCells[indexString] = cell
return cell
} else {
print("Returning contents of tableCells[\(indexString)]")
let replacementCell = tableCells[indexString] as! profileNameCell
print("tableCells[\(indexString)] name info = \(replacementCell.profileName.text ?? "")")
return replacementCell
}
【问题讨论】:
标签: ios swift uitableview dictionary