2017-02-05 49 views
0

第一次试图实现一个菜单。我有UITableView用在UITableViewController. TableView中配置自定义UITableViewCell如下:shouldShowMenuForRowAt不被称为

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未被调用。

我有一个正常工作的示例项目。但是,这不起作用。任何想法可能是什么原因?

回答

0

您应该重写UITableViewDelegate方法,而不是NSObject

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