ios - "copy"选项不显示 UITableView 行

标签 ios swift uitableview

我遇到了这个小问题,我已经实现了执行复制弹出窗口以显示用户何时复制表格 View 单元格上的项目。但是,出于某种原因,它没有显示复制弹出窗口。请看看我的代码,看看我做错了什么。

这是代码:

func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
    return action == #selector(copy(_:))
} 

这是我的完整代码

import UIKit
import CoreLocation

class PlaceDetailViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var headerView: PlaceDetailHeaderView!

    var annotation: ARAnnotation?
    var place: Place!
    var nearMeIndexSelected = NearMeIndexTitle()

    override func viewDidLoad() {
        super.viewDidLoad()
        if #available(iOS 11.0, *) {
            navigationItem.largeTitleDisplayMode = .never
        } else {
            // Fallback on earlier versions
        }

        if self.place != nil {
            self.title = place.placeName
            self.tableView.delegate = self
            self.tableView.dataSource = self
            tableView.separatorStyle = .none
            self.loadDetailInfo()
            print("Place is not nil")
        } else {
            print("Place is nil")
        }

        // Configure header view
        headerView.nameLabel.text = place?.placeName
        headerView.headerImageView.image = UIImage(named: "blurimg.jpg")
    }

    func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
        return action == #selector(copy(_:))
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.row {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PlaceDetailIconTextCell.self), for: indexPath) as! PlaceDetailIconTextCell
            cell.iconImageView.image = UIImage(named: "phone")
            cell.shortTextLabel.text = place?.phoneNumber

            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PlaceDetailIconTextCell.self), for: indexPath) as! PlaceDetailIconTextCell
            cell.iconImageView.image = UIImage(named: "map")
            cell.shortTextLabel.text = place?.address

            return cell
        case 2:
            let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PlaceDetailIconTextCell.self), for: indexPath) as! PlaceDetailIconTextCell
            cell.iconImageView.image = UIImage(named: "web")
            cell.shortTextLabel.text = place?.website

            return cell

        default:
            fatalError("Failed to instantiate the table view cell for detail view controller")
        }
    }
}

最佳答案

canPerformAction 方法的文档所示,您需要实现shouldShowMenuForRowAt 方法。

func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
     return true
}

只有在实现并返回 true 时才会调用 canPerformAction

关于ios - "copy"选项不显示 UITableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48244295/

相关文章:

ios - UITableViewCell 作为参数不是副本吗?

iphone - UISearchBar 和 resignFirstResponder

ios - XIB 中用作自定义 UITableViewCell 的自定义按钮不响应点击 (ios7)

ios - 不在 set updateConstraints 中时,UIStackView 间距会打破约束

objective-c - 需要建议在 iOS 中处理大型矢量图形

objective-c - UIApplication openURL : doesn't work with relative URLs

swift - 如何创建和更改导航栏的大小高度 iOS 11

ios - 如何在调用 SOAP 网络服务时显示事件指示器?

objective-c - 什么时候-tableView :cellForRowAtIndexPath: get called?

ios - 带有 mask 层的 UIVisualEffectView