make UITableViewCell selectable only while editing - ios

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.

Related

userInteractionEnabled disable all elements in cell

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;

Make cell not clickable, but a button on it should still be clickable

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

Can I disable the UITableView selection of the individual cell?

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;

dim everything but selected row ios

How could I easily dim the complementary set of rows, when one gets selected.
Right now I have the code to select a cell so I can call a method on it, but I would like to set opacity of all the other rows.
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SummaryCell * selectedCell = [tableView cellForRowAtIndexPath:indexPath];
[selectedCell manageContent];
}
Edit: I dont want to iterate through all the other cells (because there will be a lot of them) - wouldn't it be easier to add an UIView above all other cell (this would also prevent user interaction) and place the selected cell above that view (something like increasing z-index in HTML).
It depends what you mean by "dim" the other rows. The process to iterate over all visible rows and to set a property on all the rows other than the one selected is as follows:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
for (UITableViewCell *otherCell in self.tableView.visibleCells) {
NSIndexPath *otherIndexPath = [self.tableView indexPathForCell:otherCell];
if (![indexPath isEqual:otherIndexPath]) { // exclude the selected cell
// Do whatever you want with otherCell here
}
}
}
Where my comment is, you can set whatever properties you like on otherCell. For example, otherCell.alpha which is the alpha (transparency) of that cell.
You can iterate all the visible cells, configure them to be dimmed, then on the datasource cellForIndex method check if there's a selected cell, if there is, check if it is at the asked index, if so configure the cell as selected, if not as dimmed.

how to uncheck all cells on tableview in ios

I have a tableview, where when the user selects the cell it will set the accessorytype to UITableViewCellAccessoryCheckmark.
Now when I navigate to the previous screen, then go forward to this tableview, my cells remains checked.
Is there a way to uncheck all of them? I guess basically set all of them to UITableViewCellAccessoryNone.
I tried using reloadData, when the view appear, but that doesn't seem to trigger the cellForRowAtIndexPath (this is where my logic is to set the accessorytype of the cells)
I think this may do what you want. I assume you want to uncheck all the cells as part of responding to tableView:didSelectRowAtIndexPath: but you could insert this code anywhere:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Uncheck any visible, checked cells.
NSArray *visibleCells = [tableView visibleCells];
for (UITableViewCell *cell in visibleCells) {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
// Now do whatever else you want in response to a row being selected.
}
You should set the accessorytype in tableView:willDisplayCell:forRowAtIndexPath: instead of cellForRowAtIndexPath so that it is updated whenever it is displayed (and not just when created).
There is a tableview method called clearsselectiononviewwillappear that you might have accidentally overwritten. The default behavior should be YES. Check out Apple's doc
on UITab

Resources