【问题标题】:How to use UITableViewCell's accessoryButton as anchor for a Popover如何使用 UITableViewCell 的附件按钮作为 Popover 的锚点
【发布时间】:2017-12-25 11:49:15
【问题描述】:

我尝试使用 UITableViewCell 的 accessoryButton 作为 Popover 的锚点,但无法使用情节提要连接它。以编程方式我尝试使用accessoryButtonTappedForRowWith 函数,这也不起作用。我怎样才能连接它?

【问题讨论】:

    标签: ios swift uitableview uiviewcontroller uipopover


    【解决方案1】:

    如果您的类是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 连接起来。

    【讨论】:

    • 链接失效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 2018-10-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    相关资源
    最近更新 更多