简体   繁体   English

tableFooterView中的UIButton未注册触摸

[英]UIButton in tableFooterView not registering touches

I have a UITableView with a custom footer set to the tableFooterView ( not a sectionFooter). 我有一个UITableView并将自定义页脚设置为tableFooterView而不是 sectionFooter )。 I do this like so: 我这样做是这样的:

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")
}

Now, the button is visible and appears where I want the button to be. 现在,该按钮可见,并显示在我希望该按钮所在的位置。 I can also set the background color and it appears right behind the button, so I don't think the frame is the issue here (because lots of similar questions asked regarding this). 我还可以设置背景颜色,它会出现在按钮的后面,因此,我认为框架不是这里的问题(因为与此相关的问题很多)。 Correct me if I'm wrong. 如果我错了纠正我。 I have user interaction enabled everywhere I think it needs to be enabled. 我认为需要启用的所有地方都启用了用户交互。

Any ideas on how I can fix this? 关于如何解决此问题的任何想法? I have tried almost everything and I can't seem to figure it out. 我已经尝试了几乎所有内容,但似乎无法解决。 All of this (the UITableView ) is shown inside a UICollectionViewCell , which takes up the entire screen but allows users to swipe between the sections. 所有这些( UITableView )都显示在UICollectionViewCell内部,该UICollectionViewCell占据整个屏幕,但允许用户在各部分之间滑动。 All other buttons inside the table work fine though. 表格中的所有其他按钮都可以正常工作。

Just figured out the solution. 刚刚想出了解决方案。 Because the button is initiated in the UICollectionViewCell, referencing self doesn't work. 因为按钮是在UICollectionViewCell中启动的,所以引用self无效。 It works when I add the button action inside the init method: 当我在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