can't click UIButton inside a cell in UITableview

三世轮回 提交于 2019-12-03 06:52:16

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...

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 contentVieware 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

HRM

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.

Nick

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?

The UITableViewCell is handling all the gestures by default. Set cell.selectionStyle = UITableViewCellSelectionStyleNone; to make it disable the default behaviour.

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 was doing this:

let plusButton: UIButton = {
        let v = UIButton()
        v.setImage(plusImg, for: .normal)
        v.tintColor = .dark
        v.translatesAutoresizingMaskIntoConstraints = false
        v.addTarget(self, action: #selector(plusButtonHandler), for: .touchUpInside)
        // adding targets here did not work
        return v
    }()

And it appears invoking the addTarget method inside of the closure did not work. When I factored the addTarget method out and instead put it in the initializer, everything worked!

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];
}

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!