代码之家  ›  专栏  ›  技术社区  ›  Annabelle Sykes

tableview-未调用didselectrowatindexpath

  •  -1
  • Annabelle Sykes  · 技术社区  · 6 年前

    我有一个 UITableView 它嵌入在 UIView ,作为下拉视图(参见图片)。但是,它不响应单元用户触摸(使用 didSelectRowAtIndexPath 功能)。请看下面的代码。

    其工作原理是下拉视图是一个按钮的子视图(它是静态的 UITableViewCell ,因此当按下按钮时,下拉视图高度将从 0 88 (细胞高度也会改变)。

    我检查了视图层次结构,没有任何东西阻止视图注册触摸。

    下拉视图自定义类

    class GenderDropDownView: UIView, UITableViewDelegate, UITableViewDataSource {
    
    var genders = ["male.", "female."]
    var tableView = UITableView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        self.layer.cornerRadius = 6
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        tableView.delegate = self
        tableView.dataSource = self
        self.addSubview(tableView)
    
        // Table View constraints
        tableView.translatesAutoresizingMaskIntoConstraints = false
        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
    
        // Table view design
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.allowsSelection = true
        tableView.isUserInteractionEnabled = true
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = genders[indexPath.row]
        cell.textLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        cell.textLabel?.textColor = .white
        cell.textLabel?.textAlignment = .center
        cell.backgroundColor = .clear
        cell.selectionStyle = .none
        cell.isUserInteractionEnabled = true
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(genders[indexPath.row])
    }
    }
    

    在父级中配置下拉视图 UITableViewController

    func genderDropdownViewConfig() {
        genderDropdownView.backgroundColor = Constants.azureColor
        genderDropdownView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
        genderDropdownView.translatesAutoresizingMaskIntoConstraints = false
        genderDropdownButton.addSubview(genderDropdownView)
    
        genderDropdownButton.addSubview(genderDropdownView)
        tableView.bringSubview(toFront: genderDropdownView)
    
        genderDropdownView.topAnchor.constraint(equalTo: genderDropdownButton.bottomAnchor).isActive = true
        genderDropdownView.centerXAnchor.constraint(equalTo: genderDropdownButton.centerXAnchor).isActive = true
        genderDropdownView.widthAnchor.constraint(equalTo: genderDropdownButton.widthAnchor).isActive = true
        //genderDropdownHeight = genderDropdownView.heightAnchor.constraint(equalToConstant: 0)
        genderDropdownHeight = genderDropdownView.heightAnchor.constraint(equalToConstant: 0)
    
        for subview in genderDropdownView.subviews {
            subview.backgroundColor = .clear
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let heights = [280, 130, 103, 103, 103, 103]
        if indexPath.row ==  2 && genderIsOpen == true {
            return 191
        } else {
            return CGFloat(heights[indexPath.row])
        }
    }
    
    @objc func genderDropDownTouched() {
        if genderIsOpen == false {
            NSLayoutConstraint.deactivate([self.genderDropdownHeight])
            genderDropdownHeight.constant = 88
            genderIsOpen = true
            NSLayoutConstraint.activate([self.genderDropdownHeight])
    
            // Editing height of cell
            tableView.reloadData()
    
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
                self.genderDropdownView.layoutIfNeeded()
                self.genderDropdownView.center.y += self.genderDropdownView.frame.height / 2
            }, completion: nil)
        } else {
            NSLayoutConstraint.deactivate([self.genderDropdownHeight])
            genderDropdownHeight.constant = 0
            genderIsOpen = false
            NSLayoutConstraint.activate([self.genderDropdownHeight])
    
            // Editing height of cell
            tableView.reloadData()
    
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
                self.genderDropdownView.center.y -= self.genderDropdownView.frame.height / 2
                self.genderDropdownView.layoutIfNeeded()
            }, completion: nil)
        }
    }
    

    enter image description here

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  0
  •   matt    6 年前

    其工作原理是下拉视图是按钮的子视图

    但是下拉视图出现在按钮外面。因此,它超出了它的超视界,这意味着它不能对触摸做出反应。