ios - swift UITableView 中的 didSelectRow 事件延迟

标签 ios swift uitableview

Image

当我触摸任何单元格时,它应该被调用,但当我在任何单元格上按住触摸 5 - 6 秒时它被调用。你可以在上图中看到。

我单独创建了相同的下拉按钮,它工作正常。但是当我将它包含在我的项目中时,它无法正常工作。

UIViewController

import UIKit

class Registration: UIViewController {

    var genderDropDown: DropDownButton = {
       let dropDown = DropDownButton(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
       dropDown.setTitle("Gender", for: .normal)
       dropDown.translatesAutoresizingMaskIntoConstraints = false
       dropDown.backgroundColor = setColor(rgbValue: AppColor.pink)
       dropDown.setTitleColor(.white, for: .normal)
       return dropDown
   }()

   override func viewDidLoad() {
       super.viewDidLoad()

       registrationContainer.addSubview(genderDropDown)
       NSLayoutConstraint.activate([
           genderDropDown.topAnchor.constraint(equalTo: emailTF.bottomAnchor, constant: 30),
           genderDropDown.leftAnchor.constraint(equalTo: registrationContainer.leftAnchor, constant: 20),
           genderDropDown.heightAnchor.constraint(equalToConstant: 50),
           genderDropDown.widthAnchor.constraint(equalToConstant: 100)
       ])
    }
}

DropDownButton

class DropDownButton: UIButton, DropDownProtocol {

   func dropDownPressed(string: String) {
       self.setTitle(string, for: .normal)
       self.dismissDropDown()
   }

   var dropDown = DropDownView()
   var height = NSLayoutConstraint()

   override init(frame: CGRect) {
       super.init(frame: frame)

       self.backgroundColor = .darkGray
       dropDown = DropDownView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
       dropDown.delegate = self
       dropDown.translatesAutoresizingMaskIntoConstraints = false
   }

   override func didMoveToSuperview() {
       self.superview?.addSubview(dropDown)
       self.superview?.bringSubviewToFront(dropDown)
       dropDown.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
       dropDown.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
       dropDown.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
       height = dropDown.heightAnchor.constraint(equalToConstant: 0)
   }

   var isOpen = false
   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       if isOpen == false{
           isOpen = true
           NSLayoutConstraint.deactivate([self.height])
           if self.dropDown.tableView.contentSize.height > 150{
            self.height.constant = 150
           }
           else {
               self.height.constant = self.dropDown.tableView.contentSize.height
           }
           NSLayoutConstraint.activate([self.height])

           UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
               self.dropDown.layoutIfNeeded()
               self.dropDown.center.y += self.dropDown.frame.height / 2
           }, completion: nil)
       } else {
            isOpen = false
            NSLayoutConstraint.deactivate([self.height])
            self.height.constant = 0
            NSLayoutConstraint.activate([self.height])

            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
              self.dropDown.center.y -= self.dropDown.frame.height / 2
              self.dropDown.layoutIfNeeded()
          }, completion: nil)
      }
   }

   func dismissDropDown(){
        isOpen = false
        NSLayoutConstraint.deactivate([self.height])
        self.height.constant = 0
        NSLayoutConstraint.activate([self.height])

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
          self.dropDown.center.y -= self.dropDown.frame.height / 2
          self.dropDown.layoutIfNeeded()
        }, completion: nil)
   }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
}

DropDownView

class DropDownView: UIView, UITableViewDelegate, UITableViewDataSource{

   let items = ["Male", "Female"]
   let tableView = UITableView()
   var delegate: DropDownProtocol!

   override init(frame: CGRect) {
       super.init(frame: frame)
       tableView.delegate = self
       tableView.dataSource = self
       tableView.translatesAutoresizingMaskIntoConstraints = false
       self.addSubview(tableView)
       tableView.backgroundColor = .darkGray
       tableView.separatorStyle = .none
       tableView.allowsSelection = true
       tableView.delaysContentTouches = false
       tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
       tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
       tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
       tableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
       tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
   }

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return items.count
   }

   func numberOfSections(in tableView: UITableView) -> Int {
       return 1
   }

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       DispatchQueue.main.async {
           self.delegate.dropDownPressed(string: self.items[indexPath.row])
           self.tableView.deselectRow(at: indexPath, animated: true)
       }
   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
       cell.textLabel?.text = items[indexPath.row]
       cell.textLabel?.textColor = .white
       cell.backgroundColor = setColor(rgbValue: AppColor.pink)
       return cell
   }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
}

最佳答案

将您的代码放入 DispatchQueue 中。

 DispatchQueue.main.async {
        self.delegate.dropDownPressed(string: items[indexPath.row])
        self.tableView.deselectRow(at: indexPath, animated: true)
 }

关于ios - swift UITableView 中的 didSelectRow 事件延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58724037/

相关文章:

iphone - iPhone 3gs 4.2.1 上的 UITableViewCell 中的 CALayer 动画跳跃

ios - 在项目中使用 pod 安装 Facebook 时出现 Apple Mach-O 链接器错误

ios - Swift:如果是正斜杠,则从字符串中删除最后一个字符

iphone - float 的 Core-Data 谓词过滤

ios - 检查是否添加了支付信息

ios - 使用 youtube-ios-player-helper 播放视频

ios - 如何以编程方式删除 UITableView 的第 0 行?

ios - 为什么带有 subview Controller 的 UIViewController 有时会返回children[0].isEmpty = true?

ios - 无法在 SpriteKit Swift 中使用 SKVideoNode 播放视频

ios - 保存和加载 UITableView 数组 - iOS Swift Parse