【问题标题】:How can I use prepare for segue in a UITableViewCell class如何在 UITableViewCell 类中使用为 segue 做准备
【发布时间】:2019-09-18 00:02:06
【问题描述】:

我想在 UITableViewCell 和 UIViewController 之间发送数据

我有一个地址列表,在我的单元格中我有一个按钮(编辑),我想发送有关单元格中地址的所有信息

@IBOutlet weak var address: UILabel!
@IBOutlet weak var rfc: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.destination is ContainerVC{

        let vc = segue.destination as? ContainerVC
        vc.address = self.address.text
    }
}

方法不会覆盖其超类中的任何方法

【问题讨论】:

  • 不要那样做。所有关于地址的信息也可以在model中获得,并且必须在controller中执行segue

标签: swift uitableview segue


【解决方案1】:

让我们在你的控制器中使用 segue,它包含使用你的 UITableViewCell 的 tableview

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == segueIdentifier,
       let destination = segue.destination as? ContainerVC,
       let selectedIndex = tableView.indexPathForSelectedRow?.row
    {
        destination.address = self.addresses[selectedIndex].text
        self.navigationController?.pushViewController(destination, animated: true) // For example
    }
}

【讨论】:

    【解决方案2】:

    要将数据从单元格类传递到控制器类,请在编辑操作中使用委托方法。

    • 在 UITableViewCell 类中定义回调委托方法:

      var callBack: ((NSDictionary) -> (Void))? 根据需要使用 DS。

    • 在 Edit Action 上调用委托方法并传递数据

      @IBAction func editAction(_ sender: Any) { 回调?(字典) }

    • 在 cellForRowAt indexPath 中使用单元格对象调用委托方法

      cell.callBack = { (currentData : NSDictionary) 在 // 对数据执行动作 }

    要将数据从Controller类传递到单元类,您可以简单地在单元类中定义一个方法,并可以在cellForRowAt indexPath中调用。

    • 在单元格类中定义 SetData(dictionary)

    • 在视图控制器中

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 让 cell = tableViewOutlet.dequeueReusableCell(withIdentifier: "CellName", for: indexPath) as!单元类名称 cell.SetData(字典) 返回单元格 }

    【讨论】:

      【解决方案3】:

      您需要从实现UITableViewCellUIViewController 执行此操作:

      假设您有一个数组来填充您的 UITableView,代码将类似于:

      import UIKit
      
      class FirstViewController: UIViewController {
      
        @IBOutlet weak var tableView: UITableView!
        var addresses = [String]()
      
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
              if segue.identifier == "showAddressDetail" {
                  if let secondViewController = segue.destination as? SecondViewController {
                      secondViewController.address = sender as! String
                  }
              }
          }
      }
      
      extension FirstViewController: UITableViewDataSource, UITableViewDelegate {
      
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
               return self.addresses.count
         }
      
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
               let cell: YourTableViewCell = tableView.dequeueReusableCell(withIdentifier: "YourTableViewCell", for: indexPath) as! YourTableViewCell
               cell.address = self.addresses[indexPath.row]
               cell.delegate = self
               return cell
         }
      

      由于您必须通过UITableViewCell 上的按钮触发的操作调用您的函数,因此您可以在您的单元格上实现一个协议:

      import UIKit
      
      protocol YourTableViewCellDelegate: class {
      
          func selectedAddress(address: String)
      }
      
      class YourTableViewCellDelegate: UITableViewCell {
      
          weak var delegate: YourTableViewCellDelegate?
          var address: String
      }
      

      在您的按钮操作中,像这样调用您的委托方法:

      @IBAction func selectAddress() {
          self.delegate?.selectedAddress(address: self.address)
      }
      

      这将触发您的UIViewController 上的委托。要处理调用,不要忘记将您的单元委托分配给您的视图控制器并在您的控制器中实现您的单元委托:

      extension FirstViewController: YourTableViewCellDelegate {
      
      func selectedAddress(address: String) {
           // Do stuff with the selected address
          }
      }
      

      【讨论】:

      • 当用户单击按钮而不是用户选择行时,我需要这样做
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      相关资源
      最近更新 更多