1

我试图让一个按钮在自定义 UITableViewCell 内打开一个下拉菜单样式的 UIView。它确实会打开包含其他按钮的 UIView。伟大的。但是,唯一可点击的部分是 UITableViewCell 中的“1”的一小部分。下拉菜单的其余部分不可单击,您可以单击它进入下面的单元格。如何使自定义 UITableViewCell 中的按钮通过下拉菜单打开 UIView 并使 UIView 内的每个按钮都可点击?

这是它现在的样子。 屏幕 1 屏幕 2 屏幕 3

//The button inside the UITableViewCell:


class PriorityButton: UIButton, DropDownDelegate {
var dropDownView = DropDownView()

var height = NSLayoutConstraint()
var isOpen = false

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

    dropDownView = DropDownView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    dropDownView.delegate = self
    dropDownView.translatesAutoresizingMaskIntoConstraints = false

    //        button.setImage(UIImage(named: "UnChecked"), for: .normal)
    //        button.setImage(UIImage(named: "Checked"), for: .selected)
    self.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
    self.setTitleColor(.black, for: .normal)
    self.backgroundColor = UIColor.white
    self.layer.borderWidth = 2
    self.layer.borderColor = UIColor.black.cgColor
    self.titleLabel?.font = UIFont.init(name: "Avenir Next", size: 24)
}

override func didMoveToSuperview() {
    self.superview?.addSubview(dropDownView)
    self.superview?.bringSubviewToFront(dropDownView)

    dropDownView.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    dropDownView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    dropDownView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
    height = dropDownView.heightAnchor.constraint(equalToConstant: 0)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if isOpen == false {

        isOpen = true

        NSLayoutConstraint.deactivate([self.height])

        if self.dropDownView.priorityTableView.contentSize.height > 300 {
            self.height.constant = 300
        } else {
            self.height.constant = self.dropDownView.priorityTableView.contentSize.height
        }


        NSLayoutConstraint.activate([self.height])

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
            self.dropDownView.layoutIfNeeded()
            self.dropDownView.center.y += self.dropDownView.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.dropDownView.center.y -= self.dropDownView.frame.height / 2
            self.dropDownView.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.dropDownView.center.y -= self.dropDownView.frame.height / 2
        self.dropDownView.layoutIfNeeded()
    }, completion: nil)
}

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

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

// 下拉菜单视图

class DropDownView: UIView, UITableViewDelegate, UITableViewDataSource {

let priorityLevel = ["1", "2", "3", "4", "5"]
var priorityTableView = UITableView()
var delegate: DropDownDelegate!

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

    self.backgroundColor = .white

    priorityTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
    priorityTableView.backgroundColor = .white
    priorityTableView.delegate = self
    priorityTableView.dataSource = self
    priorityTableView.translatesAutoresizingMaskIntoConstraints = false
    self.addSubview(priorityTableView)

    priorityTableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    priorityTableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    priorityTableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    priorityTableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}

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

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)
    cell.textLabel?.text = priorityLevel[indexPath.row]
    cell.backgroundColor = .white
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("test")
}
}

预期结果:我想单击自定义 UITableViewCell 中的“1”按钮,它会显示下拉菜单 UIView,并且能够单击下拉菜单中包含的按钮。

4

1 回答 1

1

我认为问题在于下拉列表被添加到单元格的超级视图(又名 tableView)中,并且下拉列表的代表被分配给单元格本身,因此它不会响应点击手势识别器,因为表格视图不是它的代表,并且点击应该发生在单元格上,尝试将其添加到单元格的子视图而不是 superView 可能会起作用。祝你好运

于 2019-02-10T19:23:47.677 回答