ios - shouldShowMenuForRowAt 未被调用

标签 ios uitableview menu

第一次尝试实现菜单。我在 UITableViewController 中的 UITableView 中使用了自定义 UITableViewCell TableView 配置如下:

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/

相关文章:

ios - UITableViewController 不在 iOS 模拟器中显示单元格

ios - 在 tableView 和 scrollView 之间连续滚动

python - 如何将任意小部件放入 gtk.Menu?

ios - 如何在 Facebook SDK 中分享视频?

ios - 如何使用 UILongPressGestureRecognizer 计算长按按钮的时间

ios - 应用程序在 iOS 设备和 iOS 模拟器上的行为不同

html - 使一个 div 元素弹出它的父元素有溢出 :auto

ios - UICollectionView 与 NINetworkImageViews 重复图像

iOS 应用程序在大量带有 slider 的表格单元格上崩溃

javascript - 在 cookie 中保存下拉菜单选择?