[英]How to create a long press button in TableView Cell to segue
I can only create my segue function from the ViewController and I must have the longPress functions in my TableViewCell.我只能从 ViewController 创建我的 segue function,并且我的 TableViewCell 中必须有 longPress 功能。 There's no way to make reference to the segue function without getting an error.
没有任何方法可以参考segue function 而不会出错。 How would I implement this code in the cellForRow to segue when the button is held within the TableView Cell?
当按钮保持在 TableView 单元中时,我将如何在 cellForRow 中实现此代码以继续? By the way, I'm using a storyboard.
顺便说一句,我使用的是 storyboard。
@objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
print("Long Press")
//performSegue(withIdentifier: "toProfile", sender: self)
}
}
func addLongPressGesture(){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 0.75
self.expandButton.addGestureRecognizer(longPress)
I found an easy yet unorthodox solution but works like a charm:我找到了一个简单但非正统的解决方案,但就像一个魅力:
Place a second button into your TableView Cell, connect your segue, and set it to hidden.将第二个按钮放入您的 TableView 单元中,连接您的 segue,并将其设置为隐藏。 Then create your IBOutlet for it, which I named SegueButton:
然后为它创建您的 IBOutlet,我将其命名为 SegueButton:
@IBOutlet weak var LongPressButton: UIButton!
@IBOutlet weak var SegueButton: UIButton!
Now add to your awakeFromNib:现在添加到您的 awakeFromNib:
override func awakeFromNib() {
addLongPressGesture()
}
Lastly, add the long press button code:最后,添加长按按钮代码:
@objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
print("Segue was Successful")
SegueButton.sendActions(for: .touchUpInside)
}
}
func addLongPressGesture(){
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
longPress.minimumPressDuration = 0.75
self.LongPressButton.addGestureRecognizer(longPress)
}
}
You need a way to communicate from your button / cell back to your view controller.您需要一种方法来从您的按钮/单元通信回到您的视图 controller。 I suggest using a closure.
我建议使用闭包。 It would look something like this (I haven't tested this code)...
它看起来像这样(我没有测试过这段代码)......
class SomeCell: UITableViewCell {
var didLongPress: (()->Void)? = nil
@objc func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == UIGestureRecognizer.State.began {
didLongPress?()
}
}
override func prepareForReuse() {
didLongPress = nil
}
}
class TableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellId") as! SomeCell
cell.didLongPress = { [weak self] in
self?.performSegue(withIdentifier: "showDetail", sender: nil)
}
return cell
}
}
I set up the button inside the tableview cell to segue to one view controller when tapped and a different one when long pressed.我在表格视图单元格内设置了按钮,以便在轻按时切换到一个视图 controller,在长按时切换到另一个视图。
1) Create a delegate protocol. 1) 创建一个委托协议。 You can put this in the custom TableViewCell class file but not within the class itself.
您可以将其放在自定义 TableViewCell class 文件中,但不能放在 class 本身中。
protocol MyTableViewCellDelegate : class {
func didTapButton ()
func didLongPressButton ()
}
2) Inside the actual TableViewCell class, put the following: 2) 在实际的 TableViewCell class 中,放入以下内容:
weak var delegate: MyTableViewCellDelegate?
// Boilerplate code // 样板代码
@objc func didLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.began {
delegate?.didLongPressButton()
}
}
Control drag your button from your tableview cell into this file.控制将您的按钮从表格视图单元格拖动到此文件中。 Then add the additional logic.
然后添加附加逻辑。
@IBAction func expandButton(_ sender: Any) {
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))
self.addGestureRecognizer(longPress)
delegate?.didTapButton()
}
In your main ViewController file, inside cellForRowAt:在您的主 ViewController 文件中,在 cellForRowAt 内:
cell.delegate = self
Then somewhere outside the ViewController's class logic, add the following to conform to your delegate protocol and perform your segues:然后在 ViewController 的 class 逻辑之外的某个地方,添加以下内容以符合您的委托协议并执行您的转场:
extension ViewController: MyTableViewCellDelegate {
func didTapButton() {
performSegue(withIdentifier: "toInfoSegue", sender: self)
}
@objc func didLongPressButton() {
performSegue(withIdentifier: "toProfileSegue", sender: self)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.