2015-06-22 26 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

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

相關問題