如果触摸了UITableViewCell,则忽略模态

时间:2018-01-05 02:53:07

标签: ios swift uitableview

我有一个带有UITableView的模态。如果除了表视图中的UITableViewCells之外触摸屏幕上的任何地方,我想解除模态。但是,您无法为UITableView单元创建插座以在代码中引用它。目前我有这段代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch: UITouch? = touches.first

    if touch?.view != WHAT TO PUT HERE? {
        dismiss(animated: true, completion: nil)
    }
}

我不确定如何引用UITableViewCells。我该怎么做?

1 个答案:

答案 0 :(得分:0)

我找到了一个有效的解决方案。我不得不使用这种方法调整tableView的大小以适应其内容的大小:

override func viewDidAppear(_ animated: Bool) {
    //Resize the tableView height to the height of its content.
    tableView.frame.size.height = soundTableView.contentSize.height

    //Set the origin of tableView to the bottom of the screen minus a 30px margin
    //(this was my tableView's original position before resizing)
    tableView.frame.origin.y = self.view.frame.size.height - soundTableView.frame.size.height - 30
}

然后我有这个方法,如果触摸了tableView,则解除模态:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch: UITouch? = touches.first

    if touch?.view != tableView {
        dismiss(animated: true, completion: nil)
    }
}
相关问题