UITableView disable highlight between cell selection

uitableview disable highlight color
uitableview selection color
uitableview single selection programmatically
swift uitableview highlight selected row
cell.isselected not working
uitableview set selected row programmatically
highlight uitableviewcell programmatically
ios 13 cell selection

When using UITableView with selection enabled, I can select a row and it becomes visible selected with a highlight. However, when I go to select a second row, this is what happens by default:

  1. Row 1 is already selected and visibly highlighted.
  2. I press my finger down on Row #2.
  3. As my finger is still pressed down, both Rows #1 and #2 are visibly highlighted.
  4. Releasing my finger now selects Row #2 and only it is visibly highlighted.

What I'm trying to do is make it so in step 3 above, both cells aren't highlighted at the same time. Is it possible to do this?

cell.selectionStyle = UITableViewCellSelectionStyleNone;. in Swift 4 updated cell​.selectionStyle = UITableViewCell.SelectionStyle.none. Or If you want to disable selection for just some of the cells, use: cell.userInteractionEnabled = NO; As well as preventing selection, this also stops tableView:didSelectRowAtIndexPath: being called for the cells that have it set. (Credit goes to Tony Million for this answer, thanks!)

This works

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
return NO;

}

You can just set the cell's selection style to "None" from Storyboard: See here. Or from code: cell.selectionStyle  ios objective-c uitableview Question when you tap on a cell the row gets selected and highlighted.Now what i want to do is disable the highlighting but allow the selection.Is there a way around it.There is question that answers this but it disables both the selection and highlighting.

Why UITableViewCell highlight and selection styling are such a mystery? Sure, you can disable selection style, but what's the benefit? None. That setting is more like what you do when a cell selection won't take you “Sets the selected state of the cell, optionally animating the transition between states. Even being a pretty common task in the iOS programmer’s life, I repeatedly forget everything related with how to achieve UITableViewCell highlight and selection styles. So, I always have to run a…

OK, I edited this answer based on the discussion.

Assuming you are subclassing UITableViewCell, use this code in the implementation:

(e.g., CustomTableCell.m)

#define MyTableCellHighlightedNotification @"MyTableCellHighlighted" 

- (id)initWithStyle:(UITableViewCellStyle)style 
    reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        // Your custom initialization here

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(tableCellHighlighted:)
                                                     name:MyTableCellHighlightedNotification
                                                   object:nil];

    }
}

- (void) dealloc
{
    [[NSNotifcationCenter defaultCenter] removeObserver:self];

    // ...Release ivars...

    [super dealloc]
}

- (void) setHighlighted:(BOOL) highlighted
{
    // Default behaviour (defer to super)
    [super setHighlighted:highlighted];

    if(highlighted == YES){
        // De-highlight all other cells
        [[NSNotificationCenter defaultCenter] postNotificationName:MyTableCellHighlightedNotification
                                                            object:self]

    }
}

- (void)tableCellHighlighted:(NSNotification*) notification
{
    // All cells receive this notification

    if([notifcation object] != self){
        // All cells except the notification sender de-highlight themselves
        [self setHighlighted:NO];
    }
}

ios ,When I tap a row in a UITableView , the row is highlighted and selected. Method- 3 You can also disable selection of row from interface  disable the uitableview highlighting but allow the selection of individual cells (8) when you tap on a cell the row gets selected and highlighted.Now what i want to do is disable the highlighting but allow the selection.Is there a way around it.There is question that answers this but it disables both the selection and highlighting.

Determines whether users can select a row when the table isn't in editing mode. If you're using a UITableViewController to display a table view, you get the if selectedFilterRow == indexPath.row { return } // Remove the checkmark from the​  The solution is to use the Cell Selection Callback with two UITables in your GUI. Make the first table hold the data of {'a','b','c'} then in the cell selection callback, make the second UITable visible and set its data based on the cell selection properties of the first UITable.

Sets the selected state of the cell, optionally animating the transition between states. A Boolean value that indicates whether the cell is highlighted. If you want to disable the selection highlight effect entirely you can (on Android) set listview.Selectable to false (it will still respond to listview.ItemSelected). On iOS this will disable the ItemSelected event so you could leave it as true and use this custom renderer for your ViewCells:

How to give UITableViewCells a selected color other than gray. Swift version: 5.1. Paul Hudson @twostraws May 28th 2019. Ever since iOS 7.0, table view cells have been gray when tapped, even when you Available from iOS 7.0  Run the project and test it. Now you will not be able to select more than 3 items. To disable the nextButton when there is no selection, we need to detect when the cell selection is changed.

Comments
  • That's not what I'm after. That would mean no rows are selected. Follow the steps I listed to see what I mean. Remember, there's a difference between highlighted and selected.
  • Sorry for the short answer, I'm on an iPhone right now. Tomorrow I can post code if no one else answered.
  • If you deselect the row with animated:Yes you can see the fade out (it gets selected and then deselected)
  • Again that's not what I'm after. I know it's possible to deselect a cell. I'm not trying to do that. I'm trying to avoid having both cells being highlighted at the same time, which is what can happen if one is already selected and you press and hold your finger down on another one. Try it yourself.
  • Oh, I see. Try this: keep the selected NSIndexPath on an ivar. When a cell with a different index path is selected, deselect the old one (you have the index path stored so it should be OK) and store the new index path for the next round