swift – 如何从UITableViewCell获取文本字段的值?

前端之家收集整理的这篇文章主要介绍了swift – 如何从UITableViewCell获取文本字段的值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我有这个具有4个UITextField的UITableView单元格,我想在按钮点击时获取它们的值.

代码不检索任何值.

@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

解决方法

我终于通过这个简单的解决方案找到了答案.

@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)")
}

猜你在找的Xcode相关文章