ios - 从另一个 tableviewcell 快速访问一个 tableviewcell 中的文本字段值

标签 ios swift uitableview

我不确定这是否是一个奇怪的设置,但我有两个自定义 tableView 单元格 -

  1. 手机手机
  2. 电子邮箱

在 PhoneCell 中,我有两个文本字段,这些文本字段中有一些值。在 EmailCell 中,我有一个文本字段。

我现在需要在我的 EmailCell 中访问 PhoneCell 中可用的那两个文本字段的值。而且,这个 PhoneCell 可以添加四次,即我可以添加四个电话号码

这可能吗,我该如何实现?

最终,我尝试在下面做 -

  1. 获取 PhoneCell 中的 textField 值(最多 4 个数字)到 EmailCell
  2. 对于 EmailCell 中的按钮操作 (saveContactDataClicked),我需要将 phoneCell 文本字段数据和 emailCell 文本字段数据一起作为 POST 请求进行服务调用。

手机手机

class PhoneCell: ProfileCell {

    @IBOutlet weak var countryCode: UITextField!

    @IBOutlet weak var addPhoneButton: UIButton!
    @IBOutlet weak var teleNumber: UITextField! 

override func configure(withUser user: UserData, language: String, indexPath: NSIndexPath) {
    super.configure(withUser: user, language: language, indexPath: indexPath)

    if user.phones?.count == 0 {
        self.countryCode.text = ""
        self.teleNumber.text = ""
    }

    else {
        if let userPhoneInfo = user.phones {

            self.countryCode.text = userPhoneInfo[indexPath.row].country
            self.teleNumber.text = userPhoneInfo[indexPath.row].number

        }
    }
}


}

我在 EmailCell 中尝试这样做,但无法在 phoneCell 中获取 textField 值

class EmailCell: ProfileCell {

    @IBOutlet weak var saveButton: UIButton!
    @IBOutlet weak var emailLabel: UILabel!

    @IBOutlet weak var emailText: UITextField!

    @IBOutlet weak var cautionLabel: UILabel!        

    @IBOutlet weak var cautionTextView: UITextView!       

    @IBAction func saveContactDataClicked(sender: AnyObject) {

        let phoneCell = PhoneCell()

        let phoneCode = phoneCell.countryCode.text
        let phoneNumber = phoneCell.teleNumber.text

        let data = ContactInfoData(phoneCode: phoneCode!,
                                   phoneNumber: phoneNumber!,
                                   emailAddress: emailText.text!)

        delegate?.saveEdits(forContactInfoCell: self, withData: data)
    }

    override func configure(withUser user: UserData, language: String, indexPath: NSIndexPath) {
        super.configure(withUser: user, language: language, indexPath: indexPath)

        emailText.text = user.email

    }

最佳答案

首先,您应该在phoneViewController 中声明您的emailCell,然后您可以在phoneViewController 中访问它的值。您不应该在 customCell 类中执行这些操作。这是为了查看而不是控制事物。

phoneViewController 中创建 cellForRowAt indexPath 函数并像这样声明 2 个单元格:

let phoneCell: phoneCell = tableView.dequeueReusableCell(withIdentifier: "phoneCell") as! PhoneCell
let emailCell: emailCell = tableView.dequeueReusableCell(withIdentifier: "emailCell") as! EmailCell

现在您可以像这样访问您的 textField 值:

let phoneCode = emailCell.countryCode.text
let phoneNumber = emailCell.teleNumber.text

你也可以倒过来做。我希望你明白这一点。

关于ios - 从另一个 tableviewcell 快速访问一个 tableviewcell 中的文本字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41378380/

相关文章:

ios - Int 在 UILabel 中没有达到 11

iphone - 如何在方向更改后将 XIB View 上的按钮保持在相同的相对位置?

ios - 根据用户的位置检索地理定位图像

iphone - 自动加载 UITableViewController 的 XIB

objective-c - 两个相同选项卡 View 中的 UITableView 表现不同

ios - 使用 scrollViewDidScroll 移动 ChildViewController 的 subview

ios swift - 将uiimage存储在文档目录和核心数据中的路径

ios - 打开视频耳机没声音

ios - 将 Json 数组中的对象解析为字典 Swift

iOS :How to get Facebook Album Photo's Picker