繁体   English   中英

tableFooterView中的UIButton未注册触摸

[英]UIButton in tableFooterView not registering touches

我有一个UITableView并将自定义页脚设置为tableFooterView而不是 sectionFooter )。 我这样做是这样的:

override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()
    MyTableView.tableFooterView = tableFooter 
    MyTableView.tableFooterView?.addSubview(nextButton)
}

var tableFooter: UIView = {
    let tableFooter = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64)
    tableFooter.backgroundColor = .white
    tableFooter.isUserInteractionEnabled = true
    return tableFooter
}()

var nextButton: UIButton = {
    let nextButton = UIButton(frame: CGRect(x: normalSpacing, y: 0, width: UIScreen.main.bounds.width - 32, height: 64)
    nextButton.backgroundColor = UIColor.green
    nextButton.setTitle("Next", for: .normal)
    nextButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12)
    nextButton.setTitleColor(UIColor.white, for: .normal)
    nextButton.setTitleColor(UIColor.white.withAlphaComponent(0.75), for: .selected)
    nextButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    nextButton.layer.cornerRadius = 20
    nextButton.isUserInteractionEnabled = true
    nextButton.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
    return nextButton
}()

@objc func nextAction() {
    print("Next tapped")
}

现在,该按钮可见,并显示在我希望该按钮所在的位置。 我还可以设置背景颜色,它会出现在按钮的后面,因此,我认为框架不是这里的问题(因为与此相关的问题很多)。 如果我错了纠正我。 我认为需要启用的所有地方都启用了用户交互。

关于如何解决此问题的任何想法? 我已经尝试了几乎所有内容,但似乎无法解决。 所有这些( UITableView )都显示在UICollectionViewCell内部,该UICollectionViewCell占据整个屏幕,但允许用户在各部分之间滑动。 表格中的所有其他按钮都可以正常工作。

刚刚想出了解决方案。 因为按钮是在UICollectionViewCell中启动的,所以引用self无效。 当我在init方法中添加按钮动作时,它会起作用:

MyTableView.tableFooterView?.addSubview(nextButton)
nextButton.addTarget(self, action: #selector(nextAction), for: .touchUpInside)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM