首页 > 解决方案 > swift UITableView中的didSelectRow事件延迟

问题描述

图片

当我触摸任何单元格时,它应该被调用,但是当我在任何单元格上保持触摸 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)
       ])
    }
}

下拉按钮

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")
   }
}

下拉视图

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")
   }
}

标签: iosswiftuitableview

解决方案


将您的代码放入 DispatchQueue。

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

推荐阅读