1

I have a UITableView with a custom footer set to the tableFooterView (not a 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. All other buttons inside the table work fine though.

Nirav Kotecha
  • 2,493
  • 1
  • 12
  • 26
PennyWise
  • 595
  • 2
  • 12
  • 37

1 Answers1

2

Just figured out the solution. Because the button is initiated in the UICollectionViewCell, referencing self doesn't work. It works when I add the button action inside the init method:

MyTableView.tableFooterView?.addSubview(nextButton)
nextButton.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
PennyWise
  • 595
  • 2
  • 12
  • 37