如何在 uicollectionviewcell 中保存文本字段值 [英] How to save text field value in uicollectionviewcell
问题描述
我在 uicollectionviewcell 中有文本字段
hi i have text field in uicollectionviewcell
所以我需要它的例子:
当我编辑第 5 行中的文本字段值并完成它并转到第 20 行中的文本文件以编辑值时,collectionview 已重新加载并忘记了第 5 行中的值,
when i edit text filed value in row 5 and i done it and go to text filed in row 20 to edit value the collectionview has reloaded and forget value in row 5,
所以我需要临时保存值的方法,同时手动更改它
so i need way to save value temporarily while do I change it manually
这是我的代码:
cell.foodNumber.tag = indexPath.row
if let foodcodes = self.menu![indexPath.row]["code"] as? NSString {
if contains(self.indexPathsForSelectedCells, indexPath) {
cell.currentSelectionState = true
cell.foodNumber.enabled = true
cell.foodNumber.text = "1"
println("foods:\(foodcodes) Count:\(cell.foodNumber.text)")
println(cell.foodNumber.tag)
} else {
cell.foodNumber.enabled = false
cell.foodNumber.text = nil
}
}
推荐答案
在你的 ViewController 中实现 UITextFieldDelegate 协议,特别是 textField:didEndEditing 方法.
Implement in your ViewController the UITextFieldDelegate protocol, specifically the textField:didEndEditing method.
将您的 indexPath.row 保存在 textField.tag 中,并将委托设置为控制器,您可以在其中保存值.
Save your indexPath.row in the textField.tag as you're doing, and set the delegate to the controller, where you can save the value.
这是一个非常简单的例子:
This is a very simplistic example:
class MyViewController : UITableViewController {
var texts = [Int:String]()
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier( "Cell" ) as! UITableViewCell
cell.textField.delegate = self
cell.textField.tag = indexPath.row
// restore saved text, if any
if let previousText = texts[indexPath.row] {
cell.textField.text = previousText
}
else {
cell.textField.text = ""
}
// rest of cell initialization
return cell
}
}
extension MyViewController : UITextFieldDelegate {
func textFieldDidEndEditing(textField: UITextField) {
// save the text in the map using the stored row in the tag field
texts[textField.tag] = textField.text
}
}
这篇关于如何在 uicollectionviewcell 中保存文本字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!