Home > other >  Call contextMenuConfigurationForRowAt on Click instead of Long Press
Call contextMenuConfigurationForRowAt on Click instead of Long Press

Time:03-17

I took my lead from this tutorial: https://kylebashour.com/posts/context-menu-guide

I thought I had everything, yet when I click on the row, nothing happens

UPDATE: Thanks a comment I learned that contextMenuConfigurationForRowAt is called on a long press. Is there a way to call this on a click? I wanted to give users several options for contacting the people listed. But buttons in UITableViewCell doesn't seem to work, so a context menu is the best I can think of, but a long press isn't obvious for interacting with this table view.

import UIKit

class PeerSupportVC: UIViewController {

@IBOutlet weak var peerSupportTableView: UITableView!
var supportArray: NSArray = [];
fileprivate var configuration = Configuration.sharedInstance;

override func viewDidLoad() {
    super.viewDidLoad()
    self.peerSupportTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    self.peerSupportTableView.delegate = self;
    self.peerSupportTableView.dataSource = self;
    self.peerSupportTableView.rowHeight = 200.0
    getPeerSupport();
    
}



func getPeerSupport(){
    
    self.supportArray = ["One","Two","Three"]
    DispatchQueue.main.async(execute: {
        self.peerSupportTableView.reloadData()
    });
}


}




extension PeerSupportVC: UITableViewDelegate {


func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
        let item = self.supportArray[indexPath.row]

        return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { suggestedActions in

            let phoneAction = UIAction(title: "Phone Call", image: UIImage(systemName: "person.fill")) { (action) in
                DispatchQueue.main.async(execute: {
                    print("Wants to Phone Call");
                });

            }

            let textAction = UIAction(title: "Text Messsage", image: UIImage(systemName: "person.badge.plus")) { (action) in

                DispatchQueue.main.async(execute: {
                    print("Wants to Text Message");
                });
            }

            return UIMenu(title: "Contact Options", children: [phoneAction, textAction])
        }
    }


}


extension PeerSupportVC: UITableViewDataSource{



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("Count of Support Array:",self.supportArray.count);
    return self.supportArray.count;
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath);
    for viewToRemove in cell.subviews {
        viewToRemove.removeFromSuperview();
    }
    let width = self.peerSupportTableView.frame.width;
    
    if let result = self.supportArray[indexPath.row] as? String{
        print(result);
        
        
    
        let NameLabel = UILabel(frame: CGRect(x: 180, y: 0, width: width-155, height: 30));
        NameLabel.textColor = .black;
        NameLabel.text = " " (result);
        NameLabel.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight(rawValue: 400));
        NameLabel.adjustsFontSizeToFitWidth = true;
        cell.addSubview(NameLabel);

        
        
    }


    return cell;
}


}

I dumbed the code down to share, but this lesser version still doesn't work.

What I want to happen is a menu to pop up on the line that was selected and give the user options on how to contact the person they selected. Any help on displaying the menu would be greatly appreciated.

CodePudding user response:

For reference, here's your code, modified to use a reusable cell:

// simple cell class, based on your code
class PeerCell: UITableViewCell {
    
    let nameLabel = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        commonInit()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    func commonInit() {
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        nameLabel.font = .systemFont(ofSize: 12, weight: UIFont.Weight(rawValue: 400))
        contentView.addSubview(nameLabel)
        let g = contentView.layoutMarginsGuide
        NSLayoutConstraint.activate([
            nameLabel.topAnchor.constraint(equalTo: g.topAnchor),
            nameLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 180.0),
            nameLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor),
            nameLabel.heightAnchor.constraint(equalToConstant: 30.0),
        ])
    }
    
}

class PeerSupportVC: UIViewController {
    
    @IBOutlet var peerSupportTableView: UITableView!
    var supportArray: [String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // register our custom cell
        self.peerSupportTableView.register(PeerCell.self, forCellReuseIdentifier: "Cell")
        self.peerSupportTableView.delegate = self;
        self.peerSupportTableView.dataSource = self;
        self.peerSupportTableView.rowHeight = 200.0
        getPeerSupport();
    }
    
    func getPeerSupport(){
        self.supportArray = ["One","Two","Three"]
    }
    
}

extension PeerSupportVC: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
        let item = self.supportArray[indexPath.row]
        
        return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { suggestedActions in
            let phoneAction = UIAction(title: "Phone Call", image: UIImage(systemName: "person.fill")) { (action) in
                print("Wants to Phone Call");
            }
            
            let textAction = UIAction(title: "Text Messsage", image: UIImage(systemName: "person.badge.plus")) { (action) in
                print("Wants to Text Message");
            }
            
            return UIMenu(title: "Contact Options", children: [phoneAction, textAction])
        }
    }
    
}

extension PeerSupportVC: UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("Count of Support Array:",self.supportArray.count);
        return self.supportArray.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! PeerCell
        
        let result = self.supportArray[indexPath.row]
        print(result)
        cell.nameLabel.text = result
        
        return cell;
    }
    
}

As I said in my comment, though, your contextMenuConfigurationForRowAt was working as-is -- just need to long-press on the cell to trigger the call.

  • Related