Swift 如何在uicollectionviewcell中保存文本字段值

Swift 如何在uicollectionviewcell中保存文本字段值,swift,uitextfield,uicollectionviewcell,Swift,Uitextfield,Uicollectionviewcell,嗨,我在uicollectionviewcell中有文本字段 所以我需要它的例子: 当我编辑第5行中的文本字段值并执行此操作,然后转到第20行中的文本字段编辑collectionview已重新加载的值并忘记第5行中的值时 所以我需要一种临时保存值的方法,而我需要手动更改它吗 这是我的代码: cell.foodNumber.tag = indexPath.row if let foodcodes = self.menu![indexPath.row]["code"] as? NS

嗨,我在uicollectionviewcell中有文本字段

所以我需要它的例子:

当我编辑第5行中的文本字段值并执行此操作,然后转到第20行中的文本字段编辑collectionview已重新加载的值并忘记第5行中的值时

所以我需要一种临时保存值的方法,而我需要手动更改它吗

这是我的代码:

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:DiEndediting方法

在执行此操作时,将indexPath.row保存在textField.tag中,并将委托设置为控制器,以便保存值

这是一个非常简单的例子:

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
  }
}