将数据从 CustomTableView Class 发送到其父视图控制器

Send data from CustomTableView Class to its parent View Controller

我有一个视图控制器 "VCInicio",它的视图中有一个 TableView,该 TableView 的单元格采用 .xib 格式,单元格有一个名为 [=37] 的自定义 class =],里面有我每次点击 RadioButton 时获取并打印字符串(Phone 数字)的逻辑,它会打印它的 Phone 数字,我能够打印值(来自CustomiseTableViewCell Class) 并在控制台上查看该值,但我需要将该值发送回 "VCInicio" 以便我可以从该控制器对其进行操作。我看过很多建议使用 Protocols 的示例,但我无法使它们起作用。

EDIT:由于我使用的结构,我无法使用 didSelectRowAt,因此我正在使用选择单选按钮而不是选择单元格。

解决问题的原因:

"CustomiseTableViewCellDelegate" TableView 自定义 Class(子 Class)

//Protocol Implementation
protocol CustomiseTableViewCellDelegate {
    func onPhoneNumberClicked(_ cell: CustomiseTableViewCell, phoneNumber: String?)
}

class CustomiseTableViewCell: UITableViewCell {

    var delegate: CustomiseTableViewCellDelegate?
    var phone: String?

    override func awakeFromNib() {
        super.awakeFromNib()
        ...
     }

     //Here I get and send the phoneNumber 
     @objc func radioButtonTapped(_ radioButton: UIButton) {
        ...
        phone = itemLabel.text!
        self.delegate?.onPhoneNumberClicked(self, phoneNumber: phone!)
        ...
    }
}

"VCInicio" 视图控制器

class VCInicio: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomiseTableViewCellDelegate {
    func onPhoneNumberClicked(_ cell: CustomiseTableViewCell, phoneNumber: String?) {
        //Here I print the phoneNumber
        print("From VCInicio", phoneNumber)
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "phoneCell", for: indexPath) as! CustomiseTableViewCell
        cell.delegate = self

        ...
        //cell data config 
        ...

        return cell
    }
}

如果您不想使用自定义协议:

假设您将数字存储在某个模型中的变量 phoneNumbers: [String]

  1. 让你的 VC table 视图委托:
self.tableView.delegate = self
  1. 扩展 ViewController 以符合 UITableViewDelegate 协议
class VCInicio: UIViewController, UITableViewDelegate {

//...

}
  1. 实施 func tableView(UITableView, didSelectRowAt: IndexPath) 并使用选定的单元格 phone 值稍后对其进行操作
class VCInicio: UIViewController, UITableViewDelegate {

//...

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedPhoneNumber = self.model.phoneNumbers[indexPath.row]
        // manipulate selectedPhoneNumber
    }

}


使用协议:

  1. 定义单元格委托并在单击方法中使用它
protocol CustomiseTableViewCellDelegate: class {
    func onPhoneNumberClicked(_ cell: CustomiseTableViewCell, phoneNumber: String?)
}

class CustomiseTableViewCell: UITableViewCell {
    weak var delegate: CustomiseTableViewCellDelegate?
    // ... rest of your cell class

   func onPhoneClicked() {
       // this is the function where you have code to print the number to console
      print(self.phoneNumberLabel.text)
      self.delegate?.onPhoneNumberClicked(self, self.phoneNumberLabel.text)
   }

}
  1. 在您的 UITableViewDataSource 创建单元格的方法中,将 VCInicio 作为单元格的委托
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   // assuming you have your cell here
   cell.delegate = self

   return cell
}
  1. 让你的 VC 符合 CustomiseTableViewCellDelegate 协议
class VCInicio: UIViewController, CustomiseTableViewCellDelegate {

//...

    override func onPhoneNumberClicked(_ cell: CustomiseTableViewCell, phoneNumber: String?) {
        // manipulate phoneNumber
    }

}

下面的这段代码现在适用于带有手势识别器的 ImageView。这是针对 handleAvatarTap 的。正在发布全球通知。任何 ViewController 或 View 都可以收听通知,一旦选择的通知被发布,控制器就可以对此采取行动。 在下面的示例中是一个示例,将 ImageView 作为对象传递给通知。

// this is how I am creating a new notification name type
extension Notification.Name {
    static let didTapAvatar = Notification.Name("didTapAvatar")
}

// this would go within the cell where you are printing the number
     @objc func handleAvatarTap() {
        NotificationCenter.default.post(name: .didTapAvatar, object: self)
    }

// This would go in to the view controller viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(handleAvatarTap), name: .didTapAvatar, object: nil)


// This is the objc func within the view controller
 @objc fileprivate func handleAvatarTap(notification: NSNotification) {
        // checking the type of the posted notification object
        guard let avatarView = notification.object as? AvatarView else { return }
}