UITableview: How to Disable Selection for Some Rows but Not Others

how to disable cell selection in uitableview swift
uitableview deselect row on tap
ios disable row selection
uitableview can t select row
uitableviewcell disable touch
disable a uitableviewcell
uicollectionview disable selection
uitableview single selection programmatically

I am displaying in a group tableview contents parsed from XML. I want to disable the click event on it (I should not be able to click it at all) The table contains two groups. I want to disable selection for the first group only but not the second group. Clicking the first row of second group navigates to my tube player view.

How can I make just specific groups or rows selectable?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if(indexPath.section!=0)
    if(indexPath.row==0)    

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tubeUrl]];   
}

Thanks.


You just have to put this code into cellForRowAtIndexPath

To disable the cell's selection property: (while tapping the cell)

cell.selectionStyle = UITableViewCellSelectionStyleNone;

To enable being able to select (tap) the cell: (tapping the cell)

// Default style
cell.selectionStyle = UITableViewCellSelectionStyleBlue;

// Gray style
cell.selectionStyle = UITableViewCellSelectionStyleGray;

Note that a cell with selectionStyle = UITableViewCellSelectionStyleNone; will still cause the UI to call didSelectRowAtIndexPath when touched by the user. To avoid this, do as suggested below and set.

cell.userInteractionEnabled = NO;

instead. Also note you may want to set cell.textLabel.enabled = NO; to gray out the item.

UITableview: How to Disable Selection for Some Rows but Not Others, I am displaying in a group tableview contents parsed from XML. I want to disable the click event on it (I should not be able to click it at all) The  In the outline view, select Table View Cell. (The cell is nested under Table View Controller Scene > Table View Controller > Table View. You may have to disclose those objects to see the table view cell) In the Attributes inspector, find the field labeled Selection and select None. atribute inspector.


If you want to make a row (or subset of rows) non-selectable, implement the UITableViewDelegate method -tableView:willSelectRowAtIndexPath: (also mentioned by TechZen). If the indexPath should be not be selectable, return nil, otherwise return the indexPath. To get the default selection behavior, you just return the indexPath passed to your delegate method, but you can also alter the row selection by returning a different indexPath.

example:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // rows in section 0 should not be selectable
    if ( indexPath.section == 0 ) return nil;

    // first 3 rows in any section should not be selectable
    if ( indexPath.row <= 2 ) return nil;

    // By default, allow row to be selected
    return indexPath;
}

tableView(_:willDeselectRowAt:), Return an NSIndexPath object other than indexPath if you want another cell to The delegate is sent this method for the previously selected row. You can use UITableViewCell.SelectionStyle.none to disable the appearance of the cell highlight on user selection of multiple items using the multiselect gesture on table and  possible duplicate of UITableview: How to Disable Selection for Some Rows but Not Others – JosephH Jan 30 '14 at 13:36 add a comment | 9 Answers 9


Starting in iOS 6, you can use

-tableView:shouldHighlightRowAtIndexPath:

If you return NO, it disables both the selection highlighting and the storyboard triggered segues connected to that cell.

The method is called when a touch comes down on a row. Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.

UITableViewDelegate Protocol Reference

How to disable the UITableView selection in iOS?, Is it possible to disable this so tapping a row does nothing? You have to do is set the selection style on the UITableViewCell instance Note- If the cells have UIButton or any other controls, of course those controls will still work. Article Mojo will not be liable for any damages (including direct, indirect or  Implement tableView:willSelectRowAtIndexPath:delegate method of UITableViewand 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 toUITableViewCellSelectionStyleNoneas explained in the above answers.


None from the answers above really addresses the issue correctly. The reason is that we want to disable selection of the cell but not necessarily of subviews inside the cell.

In my case I was presenting a UISwitch in the middle of the row and I wanted to disable selection for the rest of the row (which is empty) but not for the switch! The proper way of doing that is hence in the method

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

where a statement of the form

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

disables selection for the specific cell while at the same time allows the user to manipulate the switch and hence use the appropriate selector. This is not true if somebody disables user interaction through the

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

method which merely prepares the cell and does not allow interaction with the UISwitch.

Moreover, using the method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

in order to deselect the cell with a statement of the form

[tableView deselectRowAtIndexPath:indexPath animated:NO];

still shows the row being selected while the user presses on the original contentView of the cell.

Just my two cents. I am pretty sure many will find this useful.

How to disable selection of a UITableViewCell in Xcode with Swift 4 , By default UITableViewCells in a UITableViewCell are selectable. Disable selection for all UITableViewCells in the UITableView; Disable If you only want to disable the selection of some of the UITableViewCells in your MyTableViewCell else {; fatalError("The dequeued cell is not an instance of " +  UITableview: How to Disable Selection for Some Rows but Not Others ; Using Auto Layout in UITableView for dynamic cell layouts & variable row heights ; disable the uitableview highlighting but allow the selection of individual cells


You trap selections with these data source methods.

– tableView:willSelectRowAtIndexPath: 
– tableView:didSelectRowAtIndexPath: 
– tableView:willDeselectRowAtIndexPath: 
– tableView:didDeselectRowAtIndexPath:

In these methods, you check if the selected row is one you want to be selectable. If it is, take an action, if not, do nothing.

Unfortunately, you can't turn off selection for just one section. It's the whole table or nothing.

You can however set the table cells selectionStyle property to UITableViewCellSelectionStyleNone. I believe that will make the selection invisible. Combined with the above methods that should make the cells look completely inert from the user's perspective.

Edit01:

If you have a table in which only some of the rows are selectable it is important that the cells of the selectable rows be visually distinct from the non-selectable rows. The chevron accessory button is the default way to do this.

However you do it, you don't want your users trying to select rows and thinking the app has malfed because the row doesn't do anything.

ios UITableview: How to Disable Selection for Some Rows but Not , how to disable cell selection in uitableview swift uitableview deselect row on tap uitableview can t select row uitableview select multiple rows programmatically UITableview: How to Disable Selection for Some Rows but Not Others Selected UItableViewCell staying blue when selected How to detect tableView cell touched or clicked in swift


iOS: Multiple Selections in Table View, ios UITableview: How to Disable Selection for Some Rows but Not Others? - (​UITableViewCell *)tableView:(UITableView *)tableView  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


How to disable cell interaciton in UITableView, I am displaying in a group tableview contents parsed from XML. I want to disable the click event on it (I should not be able to click it at all) The  How to enable move but disable delete for some row in UITableView 5 Allow UITableView to reorder, but not delete in edit mode, and enable swipe to delete anyway


Today, we will go through another common-used Table View scenario: Here is what I usually see in existing apps, or in some answers on Another solution is little nicer: get the index of currently selected TableView rows, and But it does not know if the item is selected or not, and it should not now it. Some rows in my UITableView needs to delete and move in edit mode, but some just move. In this method I can enable or disable editing for some rows. override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool{ return true } But how to disable delete for some rows with enabled move?