第一次尝试实现菜单。我在 UITableViewController 中的
TableView 配置如下:UITableView
中使用了自定义 UITableViewCell
。
fileprivate configureTableView() {
tableView.register(UINib(nibName: TransactionTableViewCell.nibName, bundle: nil), forCellReuseIdentifier: TransactionTableViewCell.identifier)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = TransactionEntryTableViewCell.defaultHeight
}
我像这样创建下面的单元格:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
guard let cell = tableView.dequeueReusableCell(withIdentifier: TransactionTableViewCell.identifier, for: indexPath) as? TransactionTableViewCell else { DLog("ERROR getting TransactionTableViewCell"); return UITableViewCell() }
let entry = entries[indexPath.row]
cell.entry = entry
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "newTransactionCell", for: indexPath)
cell.textLabel?.text = Strings.AddANewTransaction
return cell
default:
assert(false)
return UITableViewCell()
}
菜单相关方法如下:
override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
print("shouldShowMenuForRowAt")
return true
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
print("canPerformAction")
return action == MenuAction.duplicate.selector()
}
override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
print("action = \(action)")
}
一切似乎都很好;但是,长按表格时不会调用方法 shouldShowMenuForRowAt
。
我有一个运行良好的示例项目。但是,这是行不通的。任何想法可能是什么原因?
最佳答案
您应该重写 UITableViewDelegate
方法而不是 NSObject
:
override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
return action == MenuAction.duplicate.selector()
}
关于ios - shouldShowMenuForRowAt 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42053488/