在TableView中区分TableViewCell touch和UIButton

时间:2018-01-21 22:58:37

标签: ios swift uitableview

我不确定这个问题是否已经被问过但是在花了一些时间后我找不到要求Swift的人。可能会问here一个类似的问题,但对于Objective-C来说,这个问题反之亦然。

我在UIButton中有一个TableViewCell,一旦点击它就会有一些操作,但是,当点击UIButton时,只有didSelectRowAt tableView函数被触发。 UIButton位于单独的TableViewCell类中。 TableViewCell是可扩展的,因此当点击每一行时,它会展开/折叠。我确定必须有一种方法可以用UITapGestureRecognizer来控制它,但我不知道如何操纵坐标,因为我对Swift来说相对较新。

SomeTableViewCell.swift

class SomeTableViewCell: UITableViewCell {

    @IBAction func activateButtonTapped(_ sender: Any) {
        activateTapButton?(self)
    }

    var activateTapButton: ((UITableViewCell) -> Void)?
}

ViewController.swift

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet var someTableView: UITableView!

    var selectedIndex: Int = -1
    var someNumber = 123456789

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = servicesTableView.dequeueReusableCell(withIdentifier: “cell", for: indexPath) as! SomeTableViewCell
        cell.activateButton.isUserInteractionEnabled = true

        cell.activateTapButton = {(Void) in

            if let url = URL(string: "tel://\(someNumber)"), UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if(selectedIndex == indexPath.row) {
            return 280
        } else {
            return 60
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //indexPath handling once cell clicked

        // Expanding cell feature
        if (selectedIndex == indexPath.row) {
            selectedIndex = -1
        } else {
            selectedIndex = indexPath.row
        }
        self. someTableView.beginUpdates()

        self. someTableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        self. someTableView.endUpdates()

}

1 个答案:

答案 0 :(得分:0)

思考可能值得张贴答案。

这只是因为标签妨碍了UIButton,只是改变了层次结构,现在可以点击按钮。

相关问题