2016-12-19 36 views
2

所以我有這個UITableView單元有4 UITextField,我想獲得他們的值按鈕點擊。如何從UITableViewCell獲取文本字段的值?

此代碼不檢索任何值。

@IBAction func printBtnAction(_ sender: Any) { 

    let cell = self.myTableView.dequeueReusableCell(withIdentifier: "manualAddC1") as! AddFriendC1Cell 

    let alias = cell.aliasTextField.text! 
    let primaryPhone = cell.primaryPhoneTextField.text! 
    let secondaryPhone = cell.seondaryPhoneTextField.text! 
    let email = cell.emailAddressTextField.text! 

    print("Alias: \(alias), Phone: \(primaryPhone), Phone2: \(secondaryPhone), Email: \(email)") 
} 

這是TableView中 enter image description here

+0

更多細節將是有益的。 –

+0

你打印輸出?怎麼了 ?請解釋更多 –

+0

放置表格視圖的屏幕截圖 – KAR

回答

0

OK的屏幕截圖。當您按下按鈕時,您正在將一個tableViewCell出列。

let cell = self.myTableView.dequeueReusableCell(withIdentifier: "manualAddC1") as! AddFriendC1Cell 

我相信,你想獲得一個參考這樣一個現有的tableViewCell:

if let cell = tableView.cellForRow(at: indexPath) as? AddFriendC1Cell { 
     // do what you need with cell 
    } 

即使這樣的工作,我不會建議這種方法。如果用戶使用像iPhone 4這樣的小屏幕手機,並且某些單元在屏幕上不可見,該怎麼辦?我認爲在這種情況下,tableView.cellForRow會返回nil用於不可見的單元格。

我建議在每個帶有文本字段的單元格中執行textField:shouldChange,並將代表添加到tableView的viewController。當單元格中的文本發生更改時,委託應將更改傳播到viewController,該值將保存在實例變量中。

然後,當您按下按鈕時,您只需從實例變量中獲取值。

0

我想你的tableViewCells不是動態的。 你的單元格方法看起來像這樣。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellID") as! YourClass 
    cell.yourTextField.tag = indexPath.row 
// 
// Alias will be tag 0 
// Primary Phone will be tag 1 
// Secondary Phone will be tag 2 
// Email Address will be tag 3, so on and so forth 
// Then, attach to each delegate 
// 
    cell.yourTextField.delegate = self 
} 

在你的UITableView處理類(它符合UITextFieldDelegate協議)中寫下這個。

func textField(_ textField: UITextField, shouldChangeCharactersIn range:NSRange, replacementString string: String) -> Bool { 
let kActualText = (textField.text ?? "") + string 
switch textField.tag 
{ 
case 0: 
    aliasValueHolder = kActualText; 
case 1: 
    primaryPhoneValueHolder = kActualText; 
case 2: 
    secondaryPhoneValueHolder = kActualText; 
case 3: 
    emailAddressValueHolder = kActualText; 
default: 
    print("It is nothing"); 
} 
return true; 
} 

然後你可以提交它。

+0

在Swift'switch'中不需要'bre​​ak',因爲它不是貫穿,與許多語言相反。 – Moritz

+0

@EricAya,我沒有來自Swift,並且在case後面添加'break'並沒有什麼不好。這仍然是一個很好的編程習慣。 Swift仍然抱怨,如果你離開大小寫或默認沒有可執行語句。 –

7

我終於想通了這個簡單的解決方案。

@IBAction func saveBtnAction(_ sender: Any) { 

    let index = IndexPath(row: 0, section: 0) 
    let cell: AddFriendC1Cell = self.myTableView.cellForRow(at: index) as! AddFriendC1Cell 
    self.alias = cell.aliasTextField.text! 
    self.primaryPhone = cell.primaryPhoneTextField.text! 
    self.secondaryPhone = cell.seondaryPhoneTextField.text! 
    self.email = cell.emailAddressTextField.text! 

    print("Alias: \(self.alias), Phone: \(self.primaryPhone), Phone2: \(self.secondaryPhone), Email: \(self.email)") 
} 
+0

我忘了提及所有文本字段都在第一個表格單元部分和行(行:0,部分:0)中,並且我還有其他部分,但除非向下滾動,否則視圖不會顯示它。 –