ios - 如何在 segue 中获取选定的 TableView 单元格行?

标签 ios swift overriding segue indexpath

我正在使用 Swift 开发一个 iOS 应用程序,我有一个 View Controller ,它通过选择一个单元格行或选择该单元格行的内容 View 内的按钮从 TableView 单元格内容 View 中分离出来。包含 TableView 的原始 View Controller 在两种不同的情况您按下表格 View 单元格的内容 View 内的按钮。在第一个 segue 中,当我覆盖第一个 segue 时,我能够传递用 if let indexPath = self.tableview.indexPathForSelectedRow 选择的单元格行。但是,当我尝试传递在我尝试覆盖按下按钮时发生的第二个 segue 时选择的单元格行时,它不起作用。这是因为表格 View 单元格内的按钮不知道它是在哪一行中选择的吗?如果是这样,我该如何解决这个问题,如果不是,解决此类问题的可行解决方案是什么?提醒:当您选择一个单元格行时会触发“playDrill”segue,当您在同一单元格行的内容 View 中选择一个按钮时会触发“Tips”segue

当您选择一个单元格行时发生的第一个 segue 的代码(此 segue 根据需要运行):

class DrillsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "playDrill" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            if initialRow == 1 {
                drillVid = videoURL[indexPath.row]
                playerViewController = segue.destination as! PlayerController
                playerViewController.player = AVPlayer(playerItem: AVPlayerItem(url: drillVid))
                playerViewController.player?.play()
                print(indexPath) //prints correct value which is "[0,6]"
            }
            if initialRow == 3 {
                drillVid = videoUrl[indexPath.row]
                playerViewController = segue.destination as! PlayerController
                playerViewController.player = AVPlayer(playerItem: AVPlayerItem(url: drillVid))
                playerViewController.player?.play()
            }

当您在单元格的内容 View 中选择一个按钮时触发的第二个 segue 的代码(我希望这个 segue 与第一个 segue 一样具有 indexPath 的值,但是当我尝试使用该代码不会返回正确的值):

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "Tips" {
       if let indexPath = self.tableview.indexPathForSelectedRow {
        if initialRow == 1 {
            print(indexPath)  //the value printed is "6", I want "[0,6]" like the first code block
            let tipVC = segue.destination as! KeysController

            } 
        }
    }
}

最佳答案

几个月前我也遇到过这个问题......最后我能够通过以下任务解决它:

  1. 在您相应的 TableViewCell

    中创建按钮的 Outlet 和 Action
  2. 在您的 TableViewCell.swift 文件中创建一个 XYTableViewCellDelegate 协议(protocol)

  3. 在您的 TableViewCell 类中定义您之前创建的 TableViewCell 委托(delegate)的委托(delegate)

  4. 在 TableView 的 cellForRowAt: 函数中定义委托(delegate)

  5. 将委托(delegate)添加到还实现了 TableView 的 ViewController 类

  6. 最后只需在您的 ViewController 中创建此函数以接收点击的按钮 tableview indexpath

如果您在这方面需要更多帮助,请在此处复制/粘贴您的整个 ViewController 和 TableViewCell 类 - 然后我们可以直接在代码中进行更改。


它应该类似于下面的代码:

// FILE VIEWCONTORLLER
// -> 5.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, XYTableViewCellDelegate {

  // ... view did load stuff here

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell:TripDetailsTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

      // -> 4.
      cell.delegate = self

    // ... 
  }

  // -> 6.
  func buttonTappedViewCellResponse(cell: UITableViewCell) {
     let indexPath = tableView.indexPath(for: cell)
     // do further stuff here
  }
}


// FILE TABLEVIEWCELL
// -> 2.
protocol XYTableViewCellDelegate {
    func buttonTappedViewCellResponse(cell:UITableViewCell)
}
class XYTableViewCell: UITableViewCell {
  // -> 1.
  @IBOutlet weak var button: UIButton!

  // -> 3.
  var delegate: XYTableViewCellDelegate?

  // -> 1.
  @IBAction func buttonAction(_ sender: Any) {
    self.delegate?.buttonTappedViewCellResponse(cell: self)
  }
}

关于ios - 如何在 segue 中获取选定的 TableView 单元格行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46309733/

相关文章:

ios - 连接 IBOutlet 导致 nsunknownkey 异常

ios - 为什么 NSManagedObject 实例不持有对其 NSManagedObjectContext 的强引用?

iOS UITextView 生成所选文本的屏幕截图 - 是否可以使用 TextKit 或任何其他方法?

ios - 不使用 RestKit 缓存特定请求

ios - Swift - socket 无法连接到重复内容

swift - 如何调用 super 方法?

ios - 通过Swift滚动隐藏/取消隐藏UISearchBar

swift - Double 类型数字的字符串格式

javascript - 安全覆盖 Google Chrome 扩展中的 document.cookie

c++ - 基类方法别名