2017-07-31 70 views
0

我與自定義tableview單元格有一個文本框的問題。所以,我有一個單獨的類與文本字段的單元格,但我無法檢索我的tableview類中的數據。在tableview類和textFieldShouldReturn函數中有一個用於textfield的委託,但當按下「完成」按鈕時沒有任何反應。哪裏可以成爲問題?從自定義的uitableview單元格獲取uitextfield數據 - Swift

非常感謝!

這裏有一個表視圖代碼:

class MainTVC: UITableViewController, UITextFieldDelegate{ 

    var tableParts: [Dictionary<String, Any>] = [] 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(true) 
     tableParts = [ 
      ["type": "title", "data": "Some data", "enabled": true], 
      ["type": "date", "data": "Some data", "enabled": true], 
      ["type": "location", "data": "Loading...", "enabled": true], 
     ] 
     tableView.reloadData() 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.delegate = self 
     tableView.dataSource = self 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { return 1 } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tableParts.count } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     var cell = tableParts[indexPath.row] 
     switch(cell["type"] as! String) { 
     ... 
     case "textbox": 
      let rTemplateTextBox = tableView.dequeueReusableCell(withIdentifier: "recapTextCell", for: indexPath) as! RTextCell 
      rTemplateTextBox.textField.text = cell["data"] as? String ?? "No text" 

      rTemplateTextBox.data = cell 
      return rTemplateTextBox 
     default: 
      let cell = tableView.dequeueReusableCell(withIdentifier: "defCell", for: indexPath) 
      cell.textLabel?.text = "Unknown type of cell!" 
      return cell 
     } 

    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 
     print("Should return!") 
     return true 
    } 
} 

而對於一個小區:

class RTextCell: UITableViewCell, UITextFieldDelegate { 

    var textField = UITextField() 
    var data: Dictionary<String, Any>? 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     textField.delegate = self 

     let padding = UIView(frame: CGRect(x:0, y:0, width: 15, height: self.frame.height)) 

     textField = UITextField(frame: CGRect(x:0, y:0, width: self.frame.width, height: 40)) 
     textField.placeholder = "Your text here" 
     textField.textColor = UIColor.white 
     textField.tintColor = UIColor.white 
     textField.backgroundColor = UIColor.clear 
     textField.leftView = padding 
     textField.leftViewMode = .always 

     self.addSubview(textField) 
    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 
     print("Should return") 
     return true 
    } 

    func textFieldDidEndEditing(_ textField: UITextField) { 
     if data != nil { 
      data!["data"] = textField.text 
      print("DATA: \(data!)") 
     } else { 
      print("No data") 
     } 
    } 
} 
+0

請爲您的'UITableViewCell'子類,你的tableview控制器類都相關的代碼。 –

+0

@Cheslav我認爲我的答案解決了您的問題 –

+0

您正在RTextCell類中設置textfield.delegate。所以textFieldShouldReturn只能在該類中使用。你會在哪裏使用textfield的數據。請解釋一下? –

回答

0

第一個問題 - 在您的單元類中,在awakeFromNib()您有:

// CREATE a UITextField 
var textField = UITextField() 
var data: Dictionary<String, Any>? 

override func awakeFromNib() { 
    super.awakeFromNib() 

    // assign self as the delegate of textField 
    textField.delegate = self 

    let padding = UIView(frame: CGRect(x:0, y:0, width: 15, height: self.frame.height)) 

    // CREATE a NEW UITextField 
    textField = UITextField(frame: CGRect(x:0, y:0, width: self.frame.width, height: 40)) 

所以,當你創建新的文本框,它不再分配給它的委託。這就是爲什麼沒有觸發textFieldShouldReturntextFieldDidEndEditing

與更換UITextField(frame: ..)行:

textField.frame = CGRect(x:0, y:0, width: self.frame.width, height: 40) 

下,而不是在一個「數據對象」傳遞給然後嘗試和操作,考慮使用回撥關閉。

你的類改成這樣:

class RTextCell: UITableViewCell, UITextFieldDelegate { 

    var textField = UITextField() 

    // this will be our "call back" closure 
    var didEndEditAction : ((String)->())? 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     textField.delegate = self 

     let padding = UIView(frame: CGRect(x:0, y:0, width: 15, height: self.frame.height)) 

     // just set the frame, don't create a NEW textField 
     textField.frame = CGRect(x:0, y:0, width: self.frame.width, height: 40) 
     textField.placeholder = "Your text here" 
     textField.textColor = UIColor.white 
     textField.tintColor = UIColor.white 
     textField.backgroundColor = UIColor.clear 
     textField.leftView = padding 
     textField.leftViewMode = .always 

     self.addSubview(textField) 

    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 
     print("Should return") 
     return true 
    } 

    func textFieldDidEndEditing(_ textField: UITextField) { 
     // call back using a closure 
     didEndEditAction?(textField.text ?? "") 
    } 

} 

,對其進行配置是這樣的:

case "textbox": 
     let rTemplateTextBox = tableView.dequeueReusableCell(withIdentifier: "recapTextCell", for: indexPath) as! RTextCell 
     rTemplateTextBox.textField.text = cell["data"] as? String ?? "No text" 

     // setup the "call back" closure 
     rTemplateTextBox.didEndEditAction = { 
      (newText) in 
      let cell = self.myData[indexPath.row] 
      cell["data"] = newText 
     } 

     return rTemplateTextBox 
+0

非常感謝! – Cheslav

0

你可以得到電池textField數據的任何地方你tableView聲明只需要使用下面的代碼

let indexPath = IndexPath(row: yourRow, section: yourSection) 
let cell:InputDataCell = yourTable.cellForRow(at: indexPath) as! InputDataCell 

if (cell.yourTextFieald.text?.characters.count)! { 
    print(cell.yourTextFieald.text!)       
} 
相關問題