如何在 TableView Cell 中创建一个长按按钮来继续
How to create a long press button in TableView Cell to segue
我只能从 ViewController 创建我的 segue 函数,而且我的 TableViewCell 中必须有 longPress 函数。没有办法在不出错的情况下引用 segue 函数。当按钮保持在 TableView 单元格中时,我将如何在 cellForRow 中实现此代码以继续?顺便说一句,我正在使用故事板。
@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)
您需要一种从按钮/单元格返回到视图控制器的方式。我建议使用闭包。它看起来像这样(我还没有测试过这段代码)...
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
}
}
我将 tableview 单元格内的按钮设置为在点击时切换到一个视图控制器,在长按时切换到另一个视图控制器。
1) 创建委托协议。您可以将它放在自定义 TableViewCell class 文件中,但不能放在 class 本身中。
protocol MyTableViewCellDelegate : class {
func didTapButton ()
func didLongPressButton ()
}
2) 在实际的 TableViewCell class 中,放入以下内容:
weak var delegate: MyTableViewCellDelegate?
// 样板代码
@objc func didLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.began {
delegate?.didLongPressButton()
}
}
控制将您的按钮从您的表格视图单元格拖到此文件中。然后添加附加逻辑。
@IBAction func expandButton(_ sender: Any) {
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))
self.addGestureRecognizer(longPress)
delegate?.didTapButton()
}
在您的主 ViewController 文件中,在 cellForRowAt:
内
cell.delegate = self
然后在 ViewController 的 class 逻辑之外的某处,添加以下内容以符合您的委托协议并执行您的转场:
extension ViewController: MyTableViewCellDelegate {
func didTapButton() {
performSegue(withIdentifier: "toInfoSegue", sender: self)
}
@objc func didLongPressButton() {
performSegue(withIdentifier: "toProfileSegue", sender: self)
}
}
我找到了一个简单但非正统的解决方案,但效果很好:
在您的 TableView Cell 中放置第二个按钮,连接您的 segue,并将其设置为隐藏。然后为其创建您的 IBOutlet,我将其命名为 SegueButton:
@IBOutlet weak var LongPressButton: UIButton!
@IBOutlet weak var SegueButton: UIButton!
现在添加到你的 awakeFromNib:
override func awakeFromNib() {
addLongPressGesture()
}
最后添加长按按钮代码:
@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)
}
}
我只能从 ViewController 创建我的 segue 函数,而且我的 TableViewCell 中必须有 longPress 函数。没有办法在不出错的情况下引用 segue 函数。当按钮保持在 TableView 单元格中时,我将如何在 cellForRow 中实现此代码以继续?顺便说一句,我正在使用故事板。
@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)
您需要一种从按钮/单元格返回到视图控制器的方式。我建议使用闭包。它看起来像这样(我还没有测试过这段代码)...
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
}
}
我将 tableview 单元格内的按钮设置为在点击时切换到一个视图控制器,在长按时切换到另一个视图控制器。
1) 创建委托协议。您可以将它放在自定义 TableViewCell class 文件中,但不能放在 class 本身中。
protocol MyTableViewCellDelegate : class {
func didTapButton ()
func didLongPressButton ()
}
2) 在实际的 TableViewCell class 中,放入以下内容:
weak var delegate: MyTableViewCellDelegate?
// 样板代码
@objc func didLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.began {
delegate?.didLongPressButton()
}
}
控制将您的按钮从您的表格视图单元格拖到此文件中。然后添加附加逻辑。
@IBAction func expandButton(_ sender: Any) {
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))
self.addGestureRecognizer(longPress)
delegate?.didTapButton()
}
在您的主 ViewController 文件中,在 cellForRowAt:
内cell.delegate = self
然后在 ViewController 的 class 逻辑之外的某处,添加以下内容以符合您的委托协议并执行您的转场:
extension ViewController: MyTableViewCellDelegate {
func didTapButton() {
performSegue(withIdentifier: "toInfoSegue", sender: self)
}
@objc func didLongPressButton() {
performSegue(withIdentifier: "toProfileSegue", sender: self)
}
}
我找到了一个简单但非正统的解决方案,但效果很好:
在您的 TableView Cell 中放置第二个按钮,连接您的 segue,并将其设置为隐藏。然后为其创建您的 IBOutlet,我将其命名为 SegueButton:
@IBOutlet weak var LongPressButton: UIButton!
@IBOutlet weak var SegueButton: UIButton!
现在添加到你的 awakeFromNib:
override func awakeFromNib() {
addLongPressGesture()
}
最后添加长按按钮代码:
@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)
}
}