swift - didSelectRowAt 中的长按操作
问题描述
我正在尝试对我的 tableviwe 实现长按手势。我已经用 Timer 实现了双击。我找到了一个有用的答案,但不知道为什么它根本不起作用。
我需要在我的桌子上同时长按和双击。
我正在尝试使用 LongPres 和 obj-c 函数来做到这一点。也许有可能以某种方式在 didSelectRowAt 中做到这一点。
也许问题是由 swift 4 和 swift 5 之间的一些错误连接引起的。
import UIKit
class TeamsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tapCount:Int = 0
var tapTimer:Timer?
var tappedRow:Int?
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longpress))
@IBOutlet weak var plusBtn: UIButton!
@IBOutlet weak var teamsTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
teamsTable.delegate = self
teamsTable.dataSource = self
teamsTable.rowHeight = 55
teamsTable.isScrollEnabled = false
teamsTable.backgroundColor = nil
teamsTable.separatorStyle = .none
teamsTable.addGestureRecognizer(longPress)
}
// MARK: - TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return teams.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "TeamsCell") as? TeamsCell {
cell.updateCell(team: teams[indexPath.row])
cell.teamsCellDelegate = self
cell.deleteButtonShowHide()
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//debugPrint("Checking for double taps here")
if(tapCount == 1 && tapTimer != nil && tappedRow == indexPath.row){
//debugPrint("double tap - Put your double tap code here")
teams[indexPath.row].name = chooseTeamName()
teamsTable.reloadData()
tapTimer?.invalidate()
tapTimer = nil
tapCount = 0
}
else if(tapCount == 0){
//debugPrint("This is the first tap. If there is no tap till tapTimer is fired, it is a single tap")
tapCount = tapCount + 1;
tappedRow = indexPath.row;
tapTimer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: #selector (tapTimerFired), userInfo: nil, repeats: false)
}
else if(tappedRow != indexPath.row){
//debugPrint("Tap on new row")
tapCount = 0;
if(tapTimer != nil){
tapTimer?.invalidate()
tapTimer = nil
}
}
}
@objc func tapTimerFired(aTimer:Timer){
//debugPrint("Timer fired, there was a single tap on indexPath.row = tappedRow")
if(tapTimer != nil){
tapCount = 0;
tappedRow = -1;
}
}
@objc func longpress(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.began {
let touchPoint = sender.location(in: teamsTable)
if let indexPath = teamsTable.indexPathForRow(at: touchPoint) {
//Action
print("Long press Pressed")
}
}
}
// MARK: - plusBtn
@IBAction func plusBtnTapped(_ sender: Any) {
plusBtnHide()
addTeam()
teamsTable.reloadData()
print(teams)
}
func plusBtnShow() {
if teams.count < 5 {plusBtn.isHidden = false}
}
func plusBtnHide() {
if teams.count == 4 { plusBtn.isHidden = true}
}
}
// MARK: - Extension
extension TeamsVC: TeamsCellDelegate {
func deleteCell() {
self.teamsTable.reloadData()
self.plusBtnShow()
}
}
解决方案
我找到了答案!
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longpress))
应该移到 viewDidLoad
推荐阅读
- c# - 使用 Ajax 重定向到授权控制器
- c# - 带有 Razor 页面的 ASP.NET Core MVC(不能从不同的模型返回对象)
- php - 如何从 PHP 中的关联数组的键中提取所有最后的值?
- powershell - Powershell:Start-Job 中的 Copy-Item 似乎可以工作,但之后文件丢失
- vba - 工作文档中的 Word VBA ActiveDocument.PageWidth = 9999999
- c# - 如何在 C# 中实现 Twilio Conference HangUpOnStar
- excel - 基于列表的VBA搜索数据
- c# - 在美国文化中显示特定时间和日期的温度的格式
- c# - VS2017 社区 - windows 通用项目不支持页面
- angular - 如何在HttpClient GET调用whit responseType arraybuffer中获得完整响应(包括标头响应)