单击而不是长按时调用 contextMenuConfigurationForRowAt
Call contextMenuConfigurationForRowAt on Click instead of Long Press
我从本教程开始:https://kylebashour.com/posts/context-menu-guide
我以为我拥有了一切,但是当我点击该行时,没有任何反应
更新:感谢评论,我了解到长按时会调用 contextMenuConfigurationForRowAt。有没有办法在点击时调用它?我想为用户提供多种联系所列人员的选项。但是 UITableViewCell 中的按钮似乎不起作用,所以上下文菜单是我能想到的最好的,但是长按对于与此 table 视图交互并不明显。
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;
}
}
我把代码简化了分享,但是这个低版本还是不行。
我想要的是在所选行上弹出一个菜单,让用户可以选择如何联系他们选择的人。非常感谢任何有关显示菜单的帮助。
作为参考,这是您的代码,已修改为使用可重复使用的单元格:
// 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;
}
}
不过,正如我在评论中所说,您的 contextMenuConfigurationForRowAt
正在工作 as-is -- 只需要 long-press 在手机上即可触发呼叫。
我从本教程开始:https://kylebashour.com/posts/context-menu-guide
我以为我拥有了一切,但是当我点击该行时,没有任何反应
更新:感谢评论,我了解到长按时会调用 contextMenuConfigurationForRowAt。有没有办法在点击时调用它?我想为用户提供多种联系所列人员的选项。但是 UITableViewCell 中的按钮似乎不起作用,所以上下文菜单是我能想到的最好的,但是长按对于与此 table 视图交互并不明显。
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;
}
}
我把代码简化了分享,但是这个低版本还是不行。
我想要的是在所选行上弹出一个菜单,让用户可以选择如何联系他们选择的人。非常感谢任何有关显示菜单的帮助。
作为参考,这是您的代码,已修改为使用可重复使用的单元格:
// 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;
}
}
不过,正如我在评论中所说,您的 contextMenuConfigurationForRowAt
正在工作 as-is -- 只需要 long-press 在手机上即可触发呼叫。