我正在解决这个小问题,我实现了执行复制弹出窗口以显示用户在表格视图单元格上复制项目的时间。但是,由于某种原因,它没有显示复制弹出窗口。请看看我的代码,看看我做错了什么。
这是代码:
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")
}
}
}
答案 0 :(得分:0)
如canPerformAction
方法的文档所示,您需要实现shouldShowMenuForRowAt
方法。
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
只有在实施此功能并返回true
时才会调用canPerformAction
。
答案 1 :(得分:0)
您必须实现每个文档中的3种方法,并且当Xcode借助其出色的自动完成实现为您设置陷阱时,您一定不要犯错并实现类似命名的方法。
这里是一个例子。
func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
if (action == #selector(UIResponderStandardEditActions.copy(_:))) {
return true
}
return false
}
func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
if let cell = tableView.cellForRow(at: indexPath) {
UIPasteboard.general.string = cell.textLabel?.text
}
}
请不要误以为使用Xcode可怕的自动完成功能并实现:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool
代替实际需要的方法:
override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool