Reputation: 517
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 file.
Could anyone give me some suggestions? Thanks in advance.
Upvotes: 50
Views: 31769
Reputation: 7571
If you have a CUSTOM cell class, simply include self.selectionStyle = .none
in awakeFromNib()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.selectionStyle = .none
}
Upvotes: 0
Reputation: 398
I had subclassed UITableViewCell and solved it by changing this code:
addSubview(myButton)
to this:
contentView.addSubview(myButton)
in my UITableViewCell subclass.
Upvotes: 2
Reputation: 39
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)
}
Upvotes: 0
Reputation: 11140
My problem was that I was adding UIButton
as subview of UIImageView
which has user interaction disabled by default. So I had to enable it:
imageView.isUserInteractionEnabled = true
Upvotes: 0
Reputation: 161
The solution to this problem is;
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell.bringSubviewToFront(cell.YOUR_UIView)
}
This works for me.
Upvotes: 5
Reputation: 706
The most obvious mistake I haven't seen described here is that you MUST have a selectedBackgroundImage for a UIButton to look different when selected. These are not like, say, UITableViewCells.
myButton.setBackgroundImage(UIImage(named: "ButtonBackgroundSelected.png"), forState: .Highlighted)
Upvotes: 2
Reputation: 1611
I checked everything but I made silly mistake of In storyboard my button's superview was below some other view. so touch was not passing to my button.
Upvotes: 0
Reputation: 9999
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+)
Upvotes: 4
Reputation: 6694
The problem is that your nib is not containing a contentView, as required since the iOS 8 SDK (I'm writing by experience, don't have a viable source, sorry).
When you add an UITableViewController
to a storyboard, an UITableViewCell is added to it's UITableView
automatically. If you take a look at this UITableView
, you'll see a contentView
in it.
So when you subclass an UITableViewCell
and create it with a .xib, you'll have to click&drag a UITableViewCell
to the xib instead of the default UIView
. Then don't forget to set the File Owner class and the UITableViewCell
class to your subclass.
Upvotes: 6
Reputation: 31
I have been get the same problem. The reason is
self.contentView.Frame.size.height
is always 44.
There is a suggestion you can set your self.contentView.Frame
to self.frame
at the overwrite method - (void)layoutSubviews
the problem will be solved
Upvotes: 3
Reputation: 3223
I had transparent view on that cell which implemented some internal features and handled on touches on self.
Upvotes: 0
Reputation: 498
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.
Upvotes: 0
Reputation: 322
In my case, I need to check the "User Interaction Enabled" for the cell itself (not just the contentView) as well. But as noted in other answers, there could be multiple reasons for why this is happening so I am sure this won't work for every case.
Upvotes: 1
Reputation: 4917
I have also struggled with this problem and I did a lot of research over the different answers on stackoverflow and internet, the solution is different depending on the source of the problem. For the most case, you need to do the following with the element that is supposed to take user action (tap/click etc.):
myButton.userInteractionEnabled = YES;
Particularly for UILabel/UIImageView, by default the userInteractionEnabled
flag is NO
.
If you are loading the view from a xib, you need make sure, from the custom cell view, content view, and the element you want the user action enabled, all have the check box checked:
In the example above, DeviceViewCell, Content View and Button element all have "User Interaction Enabled" check box checked.
If the custom cell is implemented as a sub class of UITableViewCell, and the custom cell view is loaded from another xib file dynamically, you HAVE TO DO one more thing which is confusing to most of the people but I will explain why later on:
In the custom cell's initialization method, you need to do the following:
self.contentView.userInteractionEnabled = NO
where self
is the subclass of UITableViewCell you created. Again, IF the custom cell is implemented as a sub class of UITableViewCell, and the custom cell view is loaded from another xib file DYNAMICALLY, you need to do the above. Here is the explanation:
self.contentView
is NOT the same Content View
you see in Interface Builder, it is created by the system as the default content view
that is on TOP of every other views/elements within your custom cell view, and it obscures the rest of views in the view hierarchy. Hence, you will need to disable the user interaction for it before the UI elements in the custom cell view can receive user interaction events. Hope this helps. If you have a better explanation I would love to hear it :-). Again is is my observation and there could be other reasons. Discussion is welcome!
Upvotes: 30
Reputation: 1180
Okay, I've found a solution to this issue, but it appears to be a huge hack; however, it is all I was able to come up with (nothing else here worked for me).
Overtop of my button
I've added a UITextField which delegates to the table view cell in question. That tableViewCell instance implements -(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
and returns NO
.
Upvotes: -1
Reputation: 9352
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.
Upvotes: 2
Reputation: 1977
None of these answers worked for me.
The only way I was able to fix this problem was to set the table cell selection from single to none. After that, I was able to receive button call backs.
Upvotes: 0
Reputation: 2381
When you are using Interface Builder (Storyboard) then be sure to check the User Interaction Enabled
checkbox from the Content View
Upvotes: 0
Reputation: 6760
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.
Upvotes: 4
Reputation: 99
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.
Upvotes: 2
Reputation: 189
I faced a similar issue on both UITableViewCell and UICollectionViewCell. Few steps to follow:
In short, TableView/CollectionView cells cannot take actionable subviews (UIButton, UISegmentedControl) that are auto-layout enabled. If you have a need for it, either disable auto-layouts and provide frames (or) put them inside a container view (Container view should have auto layout disabled) and add the actionable views (with auto layout) as subviews to the container view.
Upvotes: 1
Reputation: 111
In my case, everything that I would like the user to interact with is outside of the ContentView. Also I do not want the cell to be highlighted when it is selected.
All labels (Name, Categories, etc.) are immediately under "Content View". The WebsiteButton is not under "Content View", but it is under "Local Directory Table Cell".
In my tableViewController method tableView:cellForRowAtIndexPath: I have the following code: cell.contentView.userInteractionEnabled = NO;
That is all and the user is able to interact with the button. I do NOT use [cell bringSubviewToFront:cell.websiteButton];
Upvotes: 3
Reputation: 815
What worked for me is this:
contentView.userInteractionEnabled = NO;
called after loading my custom cell from nib. It looks like this content view is created and set as a top-level child view after creating views defined in the nib file.
Upvotes: 70
Reputation: 12257
Calling [cell bringSubviewToFront:button]
after loading the cell from nib solved this for me.
Upvotes: 32