2016-12-19 37 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)中,并且我还有其他部分,但除非向下滚动,否则视图不会显示它。 –