can't click UIButton inside a cell in UITableview

前端 未结 13 1800
生来不讨喜
生来不讨喜 2021-02-05 09:51

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:

相关标签:
13条回答
  • 2021-02-05 10:28

    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.

    0 讨论(0)
  • 2021-02-05 10:28

    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.

    0 讨论(0)
  • 2021-02-05 10:33

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

    0 讨论(0)
  • 2021-02-05 10:37

    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];
    }
    
    0 讨论(0)
  • 2021-02-05 10:40

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

    0 讨论(0)
  • 2021-02-05 10:40

    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.

    0 讨论(0)
提交回复
热议问题