searched already some possible fixes but all did not solve mine.
i keep clicking the cell in the uitableview rather than the buttons inside it.
here is my code:
Your UIButton
='s y offset is given as 160 and UIView's
height is just given as 120. Please change the UIButton's
y offset and try.
For Swift 3 and Storyboard
For me, I had to enable multiple touch inside the content view, and enable user interaction on everything - the table view, table view cell, content view, and UIButton. I was using storyboard.
I also set up my tableview cell as a subclass.
Since I couldn't find an answer here when I was exploring the problem, I posted another question on stack overflow about this. You can see my code and download a project with this working here: Can't click button inside UITableViewCell?
Update swift 4.2
in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
add it : cell.buttonCheckBox.isUserInteractionEnabled = false
OR, uncheck User Interaction Enabled inspector
Now you can select the button as well as a cell. This works for me :)
If you have no alternative selector for the UIButton
pressed inside some UITableViewCell
you need turn off userInteractionEnabled
for the button
cellButton.userInteractionEnabled = false
In this case any button touch will propagate to the cell touch.
There is another potential cause of problems like this one, and that is UITableViewCells
now have a contentView
. This can and will be placed in front of other views in your custom table view cells in order to pick up touches and detect cell selection. In doing so, it may block touches to any views behind it. I've especially noticed this annoyance when creating cells using nibs.
The accepted means for dealing with this is to ensure that all subviews are added not to the tableview cell itself, but to its contentView
. This is a read-only property that adjusts its own frame under certain circumstances, for example when slid sideways to reveal the delete button, or during editing mode. You should therefore ensure that any items you do add to the contentView
are intelligently sized and have the correct resizing behaviour.
Also be aware that by adding cells to the contentView
, you may yourself block touches to it and thus prevent the cell from being selected. I personally try not to allow cell selection in tableviews containing cells with custom user-enabled controls. It only leads to confusion and awkward interfaces. In fact, I'm seriously considering replacing all UITableViews
in my future projects with UICollectionViews
, which are far more adaptable.
-Ash
try this
public override void SetSelected(bool selected, bool animated)
{
// this.interestsButton.Selected = true;
if (selected)
{
// this.TextLabel.TextColor = UIColor.Red;
//this.interestsButton.ShowsTouchWhenHighlighted = true;
this.interestsButton.BackgroundColor = UIColor.Purple;
//this.interestsButton.SetTitleColor(UIColor.Red, UIControlState.Highlighted);
}
else
{
}
// cell.ContentView.UserInteractionEnabled = false;
base.SetSelected(selected, animated);
}
Tested on "Mvvmcross.iOS" only