I have one tableview and each cell contains one button. It\'s working pretty well all iOS versions but 7. I don\'t know what\'s going on. The cell is constructed in one xib
Wired enough, the actually solution for me is to go to storyboard, select the contentview in the cell, and check the "userInteractionEnabled"... otherwise even if you have enabled userInteraction at the button or the cell, contentview will always block the action.
I had same issue with a UIButton in a prototype cell in my storyboard. I solved it by checking "User Interaction Enabled" on the prototype cell. This worked on iOS 7.1.
As others have pointed out, always add subviews to the cell's content view. If you have overridden layoutSubviews
method, make sure to call the super implementation. Forgetting to call the super implementation yields unexpected results.
In Swift 5, I create a class UITableViewCell and I create a UIButton.
class DashboardingGameTableViewCell: UITableViewCell {
// MARK: Setup Views
var showInteractiveButton: UIButton = {
let interactive = UIButton()
interactive.setTitle("", for: .normal)
interactive.contentEdgeInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
interactive.setImage(UIImage(systemName: "ellipsis"), for: .normal)
interactive.tintColor = .white
interactive.backgroundColor = .clear
interactive.translatesAutoresizingMaskIntoConstraints = false
return interactive
}()
...
and on my
class DashboardingGameViewController: UIViewController, UITableViewDelegate, UITableViewDataSource...
The UIButton was not clickable
for It works I have to make...
on DashboardingGameViewController: UITableViewCell...
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.isUserInteractionEnabled = false
addSubview(showInteractiveButton)
...
on DashboardingGameViewController: UIViewController, UITableViewDelegate, UITableViewDataSource...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reusableIdentifier,
for: indexPath) as! DashboardingGameTableViewCell
cell.showInteractiveButton.addTarget(self, action: #selector(interactiveGames(_:)), for: .touchUpInside)
...
// MARK: - Private Funcs
@objc private func interactiveGames(_ button: UIButton) {
present(ShowInteractiveGameViewController(), animated: true, completion: nil)
}
I experienced the same issue. It turns out I had created a UIView object in IB and then set it's class to a subclass of UITableViewCell.
Since the object I originally created in IB was a UIView, the UITableViewCell contentView didn't get added until runtime, which happened to be on top of my UIButtons making them un-selectable.
Clearly, there are a number of things that can go wrong and some fixes work on certain iOS versions and not on others.
My problem was that I had a UIButton that was not an immediate subview of cell.contentView (i.e. it was a subview of a subview). I had to add my button directly to cell.contentView in order for it to be tappable. (iOS v8+)