Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 下拉菜单UIView从UITableviewCell中的按钮打开在单元格边界外不可单击_Ios_Swift_Iphone_Ios12_Swift5 - Fatal编程技术网

Ios 下拉菜单UIView从UITableviewCell中的按钮打开在单元格边界外不可单击

Ios 下拉菜单UIView从UITableviewCell中的按钮打开在单元格边界外不可单击,ios,swift,iphone,ios12,swift5,Ios,Swift,Iphone,Ios12,Swift5,我试图让一个按钮在自定义UITableViewCell中打开一个下拉菜单样式的UIView。它会打开包含其他按钮的UIView。伟大的但是,唯一可单击的部分是UITableViewCell中的一点点“1”。下拉菜单的其余部分不可单击,您可以通过它单击到下面的单元格。如何使自定义UITableViewCell中的按钮使用下拉菜单打开UIView,并使UIView中的每个按钮都可单击 这是它现在的样子。 预期成果: 我想单击自定义UITableViewCell中的“1”按钮,它将显示下拉菜单UIV

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

这是它现在的样子。

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

我认为问题在于,下拉菜单被添加到单元格的超级视图(也称为tableView)中下拉列表的委托被分配给单元格本身,因此它不会响应点击手势识别器,因为表格视图不是它的委托,点击应该发生在单元格上,尝试将其添加到单元格的子视图中,而不是超级视图中,这样可能行得通。幸运的是,最好添加一个选择器视图,从底部弹出一个标题为选定行标题的窗口。因为这是用户界面文档中最推荐的苹果术语。我添加了一个popover视图,现在我正试图添加一个集合视图,这样它是水平的,看起来很酷。谢谢是否添加了水平集合视图?
//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")
}
}