简体   繁体   English

如何为静态 TableView 单元格点击添加动作?

[英]How to add Action for Static TableView Cell tap?

how can i respond to cell taps in static tableview?我如何响应静态 tableview 中的单元格点击? Is there a way to link it with code?有没有办法将它与代码链接? I know that i can use a dynamic one but isn't it possible with a static one?我知道我可以使用动态的,但不能使用静态的吗?

didSelectRowAt doesn't get called didSelectRowAt没有被调用在此处输入图片说明

Code for Selecting not working选择代码不起作用

class SettingsVC: UITableViewController, UITabBarControllerDelegate {

    // MARK: - View lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("SELECTED") // not get called
    }
}

You need to either:您需要:

  1. Change SettingsVC to be a subclass of UITableViewController .将 SettingsVC 更改为UITableViewController的子类。
  2. Make SettingsVC the delegate of your table view, since tableView(_:didSelectRowAt:) is a method of UITableViewDelegate .使 SettingsVC 成为表格视图的委托,因为tableView(_:didSelectRowAt:)UITableViewDelegate一种方法。

This can be done using a Static table in a UIContainerView .这可以使用UIContainerView的静态表来完成。

Here is the Storyboard setup:这是故事板设置:

在此处输入图片说明

and how it looks at run-time:以及它在运行时的样子:

在此处输入图片说明

When you use a UIContainerView it creates an embed segue... and that will trigger a prepare call where you can set the tableView's delegate to self (if desired).当您使用UIContainerView它会创建一个embed segue... 这将触发prepare调用,您可以在其中将 tableView 的委托设置为 self(如果需要)。

Here's the code:这是代码:

import UIKit

class EmbedTestViewController: UIViewController, UITableViewDelegate {

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

        // When using UIContainerView, prepare for segue will be called on load

        // un-comment this block to use SELF as the delegate
        //  for the tableView in the embedded tableViewController

        //if let vc = segue.destination as? MyStaticTableViewController {
        //  vc.tableView.delegate = self
        //}

    }

    // this will only be called if .delegate is set as shown above
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt called in \"Parent\" for indexPath:", indexPath)
    }

}

class MyStaticTableViewController: UITableViewController {

    // this will NOT be called if .delegate is set as the "parent" view controller
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("didSelectRowAt called in \"Table View Controller\" for indexPath:", indexPath)
    }

}

Note the comments and commented lines of code in the prepare func in EmbedTestViewController .请注意EmbedTestViewController prepare func 中的注释和注释代码行。

If you run this with those lines commented, didSelectRowAt will be called in MyStaticTableViewController class.如果在注释这些行的情况下运行它,将在MyStaticTableViewController类中调用didSelectRowAt

If you un-comment those lines, didSelectRowAt will be called in EmbedTestViewController class.如果取消注释这些行,将在EmbedTestViewController类中调用didSelectRowAt


EDIT编辑

Here is a full example: https://github.com/DonMag/ContainerTableView这是一个完整的示例: https : //github.com/DonMag/ContainerTableView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM