我试图使一个按钮在自定义UITableViewCell内部打开一个样式为UIView的下拉菜单。并且确实打开了包含其他按钮的UIView。大。但是,唯一可单击的部分是UITableViewCell内部的一小部分“ 1”。下拉菜单的其余部分不可单击,您可以将其单击到下面的单元格。如何使自定义UITableViewCell中的按钮与下拉菜单一起打开UIView,并使UIView中的每个按钮都可单击?
这是现在的样子。
//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,并能够单击下拉菜单中包含的按钮。
答案 0 :(得分:1)
我认为问题在于下拉列表已添加到单元格的超级视图(又名tableView)中,并且下拉列表的委托人已分配给了单元格本身,因此它不会响应点击手势识别器,因为表格视图不是它的委托,点击应该在单元格上进行,请尝试将其添加到单元格的subView中,而不是将其添加到superView中,这可能会起作用。祝你好运