2015-06-22 27 views
1

您好,我有uicollectionviewcell文本字段如何保存在文本字段值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 
      } 

     } 

回答

0

在你的ViewController落实UITextFieldDelegate协议,特别是文本框:didEndEditing方法。

将您的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 
    } 
} 
+0

非常感谢你,它工作得很好 –

相关问题