【问题标题】:didSelectRow event delay in swift UITableViewswift UITableView中的didSelectRow事件延迟
【发布时间】:2019-11-06 06:09:46
【问题描述】:

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

下拉按钮

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

【问题讨论】:

  • 你是如何展示这个DropDownView的?
  • @Ashish 你在DropDownProtocol 中执行的操作如何,请分享该代码。
  • @SamarthKejriwal,我编辑了我的代码。这是我的完整代码。
  • @SandeepBhandari ,您现在可以在源代码中看到。

标签: ios swift uitableview


【解决方案1】:

将您的代码放入 DispatchQueue。

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

【讨论】:

  • 添加函数定义 dropDownPressed .
  • 我添加了完整的源代码。当我单独使用此代码时,它工作正常,但在另一个项目中它不能正常工作。这个问题我遇到过很多次了。
  • @AshishNimbria 你的 didSelectRowAt 在被点击时立即调用?
  • @AshishNimbria 先删除你的答案。编辑您的问题。
  • 它不会在点击时立即调用。当我按住按钮 5-6 秒时,它只会被调用。
猜你喜欢
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 2010-12-22
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
相关资源
最近更新 更多