Reputation: 499
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
?
Upvotes: 11
Views: 9300
Reputation: 5237
For some reason, did not work for me, but this did:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
return
}
}
UISwitch continued working, and row selection did not, which was exactly what I wanted.
Upvotes: 0
Reputation: 305
[Swift] What worked for me is, cell.isUserInteractionEnabled = true (I mean not to set it as false, default is true)
and set,
cell.selectionStyle = .none
Upvotes: 0
Reputation: 4437
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.
Upvotes: 2
Reputation: 466
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.
Upvotes: 3
Reputation: 355
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.
Upvotes: 0
Reputation: 3510
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
.
Upvotes: 1
Reputation: 47069
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];
Upvotes: 0
Reputation: 5232
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 :)
Upvotes: 1
Reputation: 6747
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.
Upvotes: 18
Reputation: 1827
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
.
Upvotes: 3