0

I am creating a simple to do list app and the UITableViewCell has a checkmark button in it along with a title. When clicking the checkmark button, the done status changes and the cell UI updates.

I also would like to utilise the long press gesture within the UITableViewCell to have a ContextMenu appear. Everything seems to be working, however when a user long presses over the checkmark button, the ContextMenu doesn't appear.

I assume there are competing gestures (ie the UIButton and the cell's Long Press), but I'm having trouble figuring out how to support both at the same time. I've created the test code below to simplify the issue. In storyboard the cell has a single button on it. When long pressed, the context menu does not appear (as I need it to).

I know there's a solution somewhere!

class TableViewController: UITableViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "cellTest", for: indexPath)
    }
    
    override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
        
        let actions = [UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive, handler: { action in
            return
        })]
        
        return UIContextMenuConfiguration(actionProvider: { _ in
            return UIMenu(options: .displayInline, children: actions)
        })
        
    }

}

0 Answers0