How to save text field value in uicollectionviewcell

点点圈 提交于 2019-12-14 02:18:21

问题


hi i have text field in uicollectionviewcell

so what i need it example :

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

this my code :

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
            }

        }

回答1:


Implement in your ViewController the UITextFieldDelegate protocol, specifically the textField:didEndEditing method.

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


来源:https://stackoverflow.com/questions/30980433/how-to-save-text-field-value-in-uicollectionviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!