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:
In my case I tried to added the UIButton direct to the cell by self.addSubview(myButton)
, so I changed to self.contentView.addSubview(myButton)
and it worked
It seems the contentView
was in front of the myButton
so the button won't receive the tap gesture.
I ran into this problem, but in my case it was not related to the table view, instead it was because I was using subviews in my button for which userInteractionEnabled
was true, setting that value for false (NO) for all the button's subviews fixed the issue for me.
I'm just baffled by this issue....
I managed to fix this problem by doing this on one project:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! CustomCell
cell.contentView.userInteractionEnabled = false // <<-- the solution
return cell
}
but the same did not work for a different project. I have no idea why...
Create a custom UITableViewCell. Refer this link.
Say you have a UIButton
by the name of displayButton, you can do something like this:
Inside your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
, after you create your cell,
[cell.dislayButton addTarget: self
action: @selector(replyButtonPressed:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
And the create the method like so in your ViewController:
- (void) replyButtonPressed: (id) sender withEvent: (UIEvent *) event
{
UITouch * touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location];
}
The UITableViewCell is handling all the gestures by default.
Set cell.selectionStyle = UITableViewCellSelectionStyleNone;
to make it disable the default behaviour.
I have the same issue, and somehow I figured out my mistake.
After I changed the constraints
with self(UITableViewCell)
to constraints
with contentView
of self(UITableViewCell)
,my cell's button is clickable again.