I want to disable cell interaction. In my function:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
I added
Cell.userInteractionEnabled = NO;
And cell interaction is disabled, but I want to leave active button (ButtonLabel).
Cell.userInteractionEnabled = NO;
Cell.ButtonLabel.userInteractionEnabled = YES;
The above code does not work. It is strange, because inversly is ok:
Cell.userInteractionEnabled = YES;
Cell.ButtonLabel.userInteractionEnabled = NO;
Button is disabled but cell no.
How to make my button active while cell is disabled ?
When you set userInteractionEnabled to NO on any instance of UIView (or its subclasses), it automatically disables user interaction on all its subviews. This is exactly the behaviour that you described.
I think what you want to do is to disable selection on a UITableViewCell. There are several ways to do it:
If you want to prevent selection on all cells altogether, you can
just set tableView.allowsSelection to NO.
If you want to prevent selection on certain cells only, do Cell.selectionStyle = UITableViewCellSelectionStyleNone;
And you can also override - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath; and return nil for cells that shouldn't trigger selection action (but this alone won't prevent the selection highlight from appearing.)
According to your comment, you want do disable push connection for specific cell. What you can do is check in shoudlPerformSegueWithIdentifier if that cell should perform.
You need to save which was the cell touched, and then:
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if (/*check if the selected cell should perform the segue*/) {
return YES;
}
return NO;
}
If you disable interaction on the cell this will affect all cell elements. What you want to do instead is set the cell to UITableViewCellSelectionStyleNone
This will work
[Cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Cell.ButtonLabel.userInteractionEnabled = YES;
Related
I have my table view that displays cells with some content and a custom accessory button.
When i tap a cell, a 5s work is launched. I don't want the user to tap a cell while the background work is not finished, so i do:
self.tableView.allowsSelection = NO;
It works fine, the user can't tap a cell.
I used to do:
self.tableView.userInteractionEnabled = NO;
But i want to scroll my table view while working in background. The issue is that i also can tap on the cell's accessory button.
How to avoid that not losing the table view scroll?
I could do:
- (void)didSelectAccessoryButton:(UIButton *)pButton onEvent:(id)event{
if(self.tableView.allowsSelection){
//Usual accessory button code
}
}
But the accessory button highlight would still be there, meaning at some point i'd need to do in cellForRowAtIndexPath:
[accessoryButton setShowsTouchWhenHighlighted:NO];
I just wish the allowsSelection = NO avoids the tap on accessory button too... So what's the better way ?
The best way i've found is to combine this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//...
tableView.allowsSelection = NO;
[tableView reloadData];
//...
}
with this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//...
accessoryButton.userInteractionEnabled = tableView.allowsSelection;
//or cell.accessoryView.userInteractionEnabled = tableView.allowsSelection;
//
}
And when the job is done, self.tableView.allowsSelection = YES;.
What bothers me with this solution is the need to check inside cellForRowAtIndexPath as to do so i also need to reload data; but in the end it's just 3 lines.
Thank you #virus for the simple "userInteractionEnabled to accessoryButton" idea.
I have a custom UITableViewCell. I have a UIImageView in the Cell and when I select the Image it changes like it should but the cell is also selected which I do not want. So my question is how can I make a certain area of my cell not selectable?
I tried overriding the touchesBegan or touchesEnded but that did not work because then I cannot select the cells at all.
if you only want to not show the selection color, use:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if you have something triggered by a cell tap, it will still happen, so if you want to disabled that also you need to take care of that in:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
or if it triggers a segue, in:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
In cellForRowAtIndexPath you can set userInteractionEnabled to false:
cell.userInteractionEnabled = NO;
I have a UITableView where the selectionStyle is set to UITableViewCellSelectionStyleNone because I don't want any visible change when a cell is tapped, but when it is in editing mode I do want the the cell selectable so the checkmark appears for each selected row, but using UITableViewCellSelectionStyleNone seems to keep the checkmark from appearing.
Any ideas how I can accomplish this?
Thanks...
Have you tried setting the selection properties of your tableView like this:
tableView.allowsMultipleSelection = NO;
tableView.allowsMultipleSelectionDuringEditing = YES;
tableView.allowsSelection = NO;
tableView.allowsSelectionDuringEditing YES;
If you want more fine-grain control over when selection is allowed you can override - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath in your UITableView delegate. The documentation states:
Return Value
An index-path object that confirms or alters the selected row. Return an NSIndexPath object other than indexPath if you want another cell to be selected. Return nil if you don't want the row selected.
You can have this method return nil in cases where you don't want the selection to happen.
I've a tableView with some cells. Each cell also contains a button. When the user clicks the button, the cell should be unclickable, but not the button. So when the user clicks on the button of a cell which is not clickable, this cell should be clickable again.
I tried:
cell.userInteractionEnabled = NO;
...but then the button wasn't clickable anymore.
Thanks to your effort in advance.
EDIT
I mean: When I click on a cell a new view opens. But I want, that no action happens, when the cell is not "clickable".
Unclickable in which way? If you just want the cell to not be selectable, you are probably seeking for this:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
If you want to prevent your code to be executed when the selection is disabled, just check for the selection property inside your didSelectRowAtIndexPath:method. Something like this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
//(your code opening a new view)
}
}
Remember, you still have to play with this property, setting to UITableViewCellSelectionStyleNone when you don't want the cell to be selectable, and setting back to UITableViewCellSelectionStyleBlue (or UITableViewCellSelectionStyleGray) when you want it to be selectable again.
Swift version:
cell.selectionStyle = .none
Remove selection by setting UITableViewCellSelectionStyleNone as the selectionStyle.
cell.selectionStyle = UITableViewCellSelectionStyleNone;
And do nothing in -tableView:didSelectRowAtIndexPath:
You can be selective in that delegate method for example if only the first row in the first section has the button and should do nothing :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *indexPathForDisabledCell = [NSIndexPath indexPathForRow:0
inSection:0];
if([indexPath compare:indexPathForDisabledCell] != NSOrderedSame) {
//Do whatever you do with other cells
}
}
This can also be done through Interface Builder using User Defined Runtime Attributes on the TableViewCell:
Key Path | Type | Value
selectionStyle | Number | 0
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/c/tdef/UITableViewCellStyle
When ever you tap on a row in a UITableView, the row is highlighted and selected. Is it possible to disable this so tapping a row does nothing?
cell.selectionStyle = UITableViewCellSelectionStyleNone;
or [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Further, make sure you either don't implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want to have no action if you do implement it.
Implement tableView:willSelectRowAtIndexPath: delegate method of UITableView and return nil.Returning nil from this method tells the table view to not select the row consequently tableView:didSelectRowAtIndexPath: method will not get called.But doing this does not prevent highlighting the row.To disable highlighting effect on row set cell selection style toUITableViewCellSelectionStyleNone as explained in the above answers.
Just set :
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
If you want stop highlighting use the code say: your desired value to be 19
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.row == '19')
cell.selectionStyle = UITableViewCellSelectionStyleNone;
else
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
But, if you want to control the selection of cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexpath.row == '19')
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
for Swift 3 :
cell.selectionStyle = UITableViewCellSelectionStyle.none
The accepted answer was not working for me. This problem driving me crazy for awhile because it would look like a "glitch" on any selection when the next view loaded..
My SOLUTION: I had to go to the .xib file of the actual TableViewCell (my custom cell that I built) select the cell and select the CellView. (Make sure you select the Cell, and NOT a subview within it. It will turn Blue like the picture shows). On the left side click "Selection" -> "None". This SHOULD be the exact same as doing it with code, but for some reason it just wasn't working...
This is for the case in which you have a custom cell, but I found this question when I was looking for the answer, so I thought I'd leave this here in case this helps others.
Go to your custom cell, and set selectionStyle to .none in the awakeFromNib method:
override func awakeFromNib() {
super.awakeFromNib()
// disable selection
selectionStyle = .none;
}
If you use this custom cell in multiple UITableViews, you only have to set it once in the custom cell instead of on every UITableView you use :)
for Swift 4
cell.selectionStyle = UITableViewCell.SelectionStyle.none
you could simply do this for OBJ C:
cell.selectionStyle = UITableViewCellSelectionStyleNone;