微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

didSelectRowAt

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。