Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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 如何在TableViewCell中处理自定义UIView按钮操作?_Ios_Swift_Uitableview - Fatal编程技术网

Ios 如何在TableViewCell中处理自定义UIView按钮操作?

Ios 如何在TableViewCell中处理自定义UIView按钮操作?,ios,swift,uitableview,Ios,Swift,Uitableview,如何在TableViewCell中处理自定义UIView按钮操作 我有一个带有XIB的自定义UIView,我将其添加到了UIViewControler中实现的TableView中。对于每个单元格,我在tableView函数-cellForRowAt中添加自定义UIView。一切看起来都很好,但我无法从该单元格添加的自定义UIView处理按钮操作。有人能帮我怎么做吗 编辑: 我的自定义UIView具有自己的XIB protocol TicketButtonDelegate { func s

如何在TableViewCell中处理自定义UIView按钮操作

我有一个带有XIB的自定义UIView,我将其添加到了UIViewControler中实现的TableView中。对于每个单元格,我在tableView函数-cellForRowAt中添加自定义UIView。一切看起来都很好,但我无法从该单元格添加的自定义UIView处理按钮操作。有人能帮我怎么做吗

编辑:

我的自定义UIView具有自己的XIB

protocol TicketButtonDelegate {
    func starButtonAction(_: UIButton)
}

class TicketView: UIView {

    @IBOutlet var ticketContentView : UIView!
    var delegate: TicketButtonDelegate!

    @IBOutlet weak var starButton : UIButton!

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

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    private func commonInit() {

        Bundle.main.loadNibNamed("TicketView", owner: self, options: nil)
        addSubview(ticketContentView)

        starButton.addTarget(self, action: #selector(starButtonAction(_:)), for: .touchUpInside)

        ticketContentView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 132)
    }

    @objc func starButtonAction(_ sender: UIButton) {
        delegate.starButtonAction(sender)
    }
}
我的UIViewController

class MhdDashboardBottom: UIViewController, TicketButtonDelegate {
    @IBOutlet weak var mhdTicketsTable: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mhdTicketsTable.delegate = self
        mhdTicketsTable.dataSource = self
        mhdTicketsTable.register(UINib(nibName: "MhdTicketTableCell", bundle: nil), forCellReuseIdentifier: "MhdTicketCell")
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "MhdTicketCell"

        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? MhdTicketTableCell else {
            fatalError("The dequeued cell is not an instance of MhdTicketTableCell")
        }

        let ticket = tickets[indexPath.row]
        let ticketCell = TicketView()
        ticketCell.delegate = self
        ticketCell.tag = 700
        var viewExists = false
        for view in cell.contentCellView.subviews {
            if view.tag == ticketCell.tag {
                viewExists = true
                break
            }
        }
        if viewExists == false {
            cell.contentCellView.addSubview(ticketCell)
        }
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 132
    }

    func starButtonAction(_: UIButton) {
        print("Works?")
    }
}
My MhdTicketTableCell(UITableViewCell)


与使用回调闭包的协议不同,它避免了视图层次结构数学、标记和协议声明:

  • 在视图中删除协议

  • 替换
    var委托:ticketbuttonelegate带有
    弱var回调:(()->Void)?

  • 替换

    @objc func starButtonAction(_ sender: UIButton) {
        delegate.starButtonAction(sender)
    }
    

  • 在控制器中删除

  • 替换

    ticketCell.delegate = self
    


索引路径,甚至是
票证
都被捕获。

经过很长时间的研究,我找到了解决方案

在我的TicketView中,我必须添加此功能以将触摸事件传递到子视图中

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    for subview in subviews {
        if !subview.isHidden && subview.isUserInteractionEnabled && subview.point(inside: convert(point, to: subview), with: event) {
            return true
        }
    }
    return false
}
在此之后,我删除了委托实现,只添加了

ticketCell.starButton.addTarget(self, action: #selector(starButtonAction(_:)), for: .touchUpInside)
进入UIViewControllers cellForRow函数,然后添加objc函数

@objc func starButtonAction(_ sender: UIButton) {
    print("Works?")
}

感谢您回答的问题。

请显示您的代码您可以进一步解释您的目标,即您想要的屏幕截图。我编辑了我的代码。可能我的问题是在TableView的cellForRow方法中将视图(包含我的按钮)添加到单元格视图中。我尝试了您编写的方法,但结果是一样的。我编辑了我的代码。查看UIViewController cellForRow中的方法,我在其中向单元格添加视图。可能有问题吗?是的,我很确定分配视图会导致问题。为什么不在Interface Builder中设计并连接所有内容?因为我希望单元格的视图可以作为应用程序其他部分的视图重用。我在单元格中也有按钮,可以轻松地在那里分配操作,而无需点击测试…我在自定义UIView中有按钮,自定义UIView分配了XIB视图,该视图作为子视图添加到TabvleViewCell容器中。所以触摸事件没有处理到我的自定义UIView。
ticketCell.delegate = self
ticketCell.callback = {
   print("Works?", indexPath)
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
    for subview in subviews {
        if !subview.isHidden && subview.isUserInteractionEnabled && subview.point(inside: convert(point, to: subview), with: event) {
            return true
        }
    }
    return false
}
ticketCell.starButton.addTarget(self, action: #selector(starButtonAction(_:)), for: .touchUpInside)
@objc func starButtonAction(_ sender: UIButton) {
    print("Works?")
}