how to disable UserInteraction for UITableview cell but not in custom button on cell

拈花ヽ惹草 提交于 2020-08-06 12:13:58

问题


How to disable UserInteraction for UITableview cell but not in custom button on that cell..

I am creating a button using :

    UIButton *dayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [dayButton setFrame:CGRectMake(201, 0, 30, 35)];
    [dayButton addTarget:self action:@selector(selectDayView) forControlEvents:UIControlEventTouchUpInside];
    [dayButton setBackgroundImage:[UIImage imageNamed:@"86-camera.png"] forState:UIControlStateNormal];
    dayButton.userInteractionEnabled=YES;
    [cell addSubview:dayButton];

and then I set

    cell.userInteractionEnabled=NO;

how can I get dayButtonAction ?


回答1:


It is not possible to applies disable userInteraction for UITableView and not on UITableViewCell. You can access content/controllers of UITableVie (which is sub View of UITableView) when userInteraction is enable of UITabelView. When you add any controller on UITableView actually you added it on UITableViewCell but cell is part of UITableView so when you disable userInteraction on UITableView it means it aslo set it(NO) for UITableViewCell.

And Here i aslo give you suggestion for add your UIButton to cell as a

[cell.contentView addSubView:buttonName];



回答2:


Don't disable user interaction with the whole cell. Set cell.selectionStyle = UITableViewCellSelectionStyleNone to prevent the cell selection. That way the user can still interact with the button.




回答3:


If you set the cell.userinteraction = NO then you can't touch the button because any interaction is closed for the cell and it's subviews.




回答4:


An old question, but think this might help someone. You can disable the user interaction of UITableView by setting the property of selection to no selection.

Identity Inspector




回答5:


One more option is to override touchesBegan and touchesEnded. Here's how:

1) make a custom cell with overriding functions and an indicator to apply them or not

class MyCustomTableViewCell: UITableViewCell { 
    var shouldDisableTouches: Bool = false

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !shouldDisableTouches { super.touchesBegan(touches, with: event) }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !shouldDisableTouches { super.touchesEnded(touches, with: event) }
    }
}

2) In storyboard set your cell class to this class in Custom Class > Class

3) Now in tableView(_:cellForRowAt:) you are able to turn interactions on or off

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath) as! MyCustomTableViewCell
    if <your condition here> {
         cell.shouldDisableTouches = true
    } else {
         cell.shouldDisableTouches = false
    }
}

...the "else" part is essential here, don't drop it. It is a reusable cell after all.

This kind of disabling don't affect buttons, that are located in a separate xib, registered for this cell. Haven't tested it on a cell, designed directly in the storyboard yet.




回答6:


You can set [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; also don't implement the tblView:didSelectRowAtIndexPath:. Make you button clickable (as it will be by default). Handle it's event to perform your task.

As of I understand If Superview has userintercation = no enabled than it's subview can't be interacted. In your case UITableViewCell is the superview and button is subview. So it is better to avoid setting cell.userInteractionEnabled = NO; and use UITableViewCellSelectionStyleNone.

Hope this helps :)




回答7:


If cell.userinteraction = NO then you can't touch it's subviews. Simple way is Just take on UIView with clearColour and size of the view is same as your Cell size and put it on the cell and above view you can set your UIButton. when you want to cell.userinteraction = NO then just set view.hidden=NO and when cell.userinteraction = Yes then set view.hidden=YES. so your Transparent UIView display and above view your UIButton then you can get the Click of button. no need to set cell.userinteraction.




回答8:


You can't set User Interaction Disabled on Cell because it's a Super View on which button is added.if you do,you will not able to interact with button so disable the interaction on rest of sub-views of UITableViewCell.



来源:https://stackoverflow.com/questions/17522953/how-to-disable-userinteraction-for-uitableview-cell-but-not-in-custom-button-on

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