我试图使用UITableViewCell的accessoryButton作为Popover的锚,但无法使用脚本连接它。以编程方式我尝试使用accessoryButtonTappedForRowWith函数,但这也不起作用。我如何连接?

最佳答案

如果您的类是UITableViewController的子类,则使用:

override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
    print(indexPath.row)
}

检查示例代码:
class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "\(indexPath.row)"

        cell.accessoryType = UITableViewCellAccessoryType.detailButton
        return cell
    }

    override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
        print(indexPath.row)
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
}

如果它是UIViewController的子类,那么在该方法之前不需要override,因此它将是:
func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
    print(indexPath.row)
}

在这种情况下,不要忘记将表视图的UITableViewDataSourceUITableViewDelegateViewController连接起来。

关于ios - 如何使用UITableViewCell的annexButton作为Popover的 anchor ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47968322/

10-12 07:14
查看更多