7
votes

enter image description hereHow can i remove the table view cell highlighted color in edit mode. I am using the table view property “allowsMultipleSelection” in edit mode. By default the cell is highlighted in a light blue color. Although we can change the highlighted color i have not seen any option to remove that highlighted color property. I want my table view cell checkbox selection only and not highlighted mode.

"cell.SelectedBackgroundView" allows to change the cell's background color when its selected, but i want to have only checkbox selection and no color for the cell in selected state.

Attached image of the screen. In my case the cell has a background image so on selection also I want the cell background to be same and only the checkbox selected.

7
Try cell?.selectionStyle = .none in cellForRowAtIndexPath or you can disable the selection of cell from storyboard too. - Ankit Jayaswal
@AnkitJayaswal I don't want to disable cell selection instead i want to disable the cell getting "highlighted" on selection. cell?.selectionStyle = .none disables the selection itself. - subin272
selectionStyle only denotes the style, You have few options to highlight your cell on selection. If you sets it to .none, it will only prevent to show colour on selection. But selection will work fine and you can see that by applying breakpoint in didSelectRowAtIndexPath - Ankit Jayaswal
@AnkitJayaswal I am using the default editing property given by UITableview (AllowsMultipleSelectionDuringEditing) and in this case the cell selection is handled by tableview only. So if we give ".none" the selection itself won't happen. - subin272
Can share the existing delegate-datasource methods? - Ankit Jayaswal

7 Answers

3
votes

UITableViewCell have selectionStyle property to highlight the cell on selection. These style highlight the cell with colour like:

Blue, Gray, Default(Light Gray), None

enter image description here

Upon selection of .none style, it will only prevent from highlighting the cell. It will not block the selection. You can see that by applying breakpoint in didSelectRowAt indexPath: function:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("UITableView Selected")
}
3
votes

In your cell class override this methods:

override func setHighlighted(_ highlighted: Bool, animated: Bool) {}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: false)
    let viewForHighlight = UIView()
    self.selectedBackgroundView = viewForHighlight
    if self.isEditing {
        viewForHighlight.backgroundColor = UIColor.clear
    } else {
        viewForHighlight.backgroundColor = UIColor.gray
    }
}
1
votes

I would check out this delegate method: tableView(_:shouldHighlightRowAt:) You can check to see if you're editing and suppress highlighting altogether by returning false in the desired states.

0
votes

If you are trying to completely remove the selection then you can use the below code

For Swift 3 cell.selectionStyle = UITableViewCellSelectionStyle.none

For Swift 4 cell.selectionStyle = .none

0
votes

Yet another option is to use (in Objective-C, you can do something similar in Swift, of course):

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

Based on your model or data or whatever, in this method you can check or uncheck the box in each cell, as well as turn off the highlight.

From the docs:

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

0
votes

Try this way, I've just checked it, it works as you need:

func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
        let cell = tableView.cellForRow(at: indexPath)
        let selectedColor: UIColor
        let selectedView = UIView(frame: CGRect.zero)
        if tableView.isEditing == true {

            selectedColor = .clear
        } else {
            selectedColor = .lightGray
        }
        selectedView.backgroundColor = selectedColor
        cell?.multipleSelectionBackgroundView = selectedView
        return true
    }
0
votes

In iOS 14 and above, you can now simply do this in your UITableViewCell subclass:

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    // Prevent cell highlighting while preserving selectability and separator views
    var backgroundConfig = UIBackgroundConfiguration.listPlainCell()
    backgroundConfig.backgroundColor = .clear
    backgroundConfiguration = backgroundConfig
}

For a solution that also works on iOS 13 and below, see https://stackoverflow.com/a/64582629/171933