tableFooterView中的UIButton未注册触摸

时间:2019-05-07 11:09:46

标签: ios swift uibutton tablefooterview

我有一个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内部,它占据了整个屏幕,但允许用户在各部分之间滑动。表格中的所有其他按钮都可以正常工作。

1 个答案:

答案 0 :(得分:2)

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

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