7

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.

subin272
  • 733
  • 6
  • 24
  • Try `cell?.selectionStyle = .none` in `cellForRowAtIndexPath` or you can disable the selection of cell from storyboard too. – Ankit Jayaswal Mar 27 '18 at 20:51
  • @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 Mar 28 '18 at 04:59
  • `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 Mar 28 '18 at 05:31
  • @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 Mar 28 '18 at 05:37
  • Can share the existing delegate-datasource methods? – Ankit Jayaswal Mar 28 '18 at 05:40
  • @AnkitJayaswal I got your point i am able to select the table view using "didSelectRowAtIndexPath" without any highlighted color when i give selection style ".none", but the problem is when i put the table view in edit mode (AllowsMultipleSelectionDuringEditing) at that time the checkbox selection in tableview is not working. This checkbox and selection on tapping is provided by apple when we put tableview in edit mode. – subin272 Mar 28 '18 at 05:45
  • @subin272 : It is not possible with "AllowsMultipleSelectionDuringEditing" you have to design custom cell with edit radio button. – Bucket Mar 28 '18 at 06:44
  • Possible duplicate of [disable the uitableview highlighting but allow the selection of individual cells](https://stackoverflow.com/questions/30233468/disable-the-uitableview-highlighting-but-allow-the-selection-of-individual-cells) – koen Mar 28 '18 at 12:33
  • @Koen it's not duplicate. This question differs from yours with 'edit mode'. – Daedelus Aug 03 '18 at 08:12

7 Answers7

3

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")
}
Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36
3

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
    }
}
Harman
  • 426
  • 8
  • 14
  • This works pretty well. I'm using a UITableViewController and push another view controller when normally selecting a cell. When I return to the main view controller I found that the cell was highlighted after this change. Changing setHighlighted to this seems to fix it for me. if !isEditing { super.setHighlighted(highlighted, animated: animated) } – Joe Schofield Jan 26 '22 at 20:16
2

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

Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165
1

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.

Dare
  • 2,497
  • 1
  • 12
  • 20
  • Will check it out. Thank you. – subin272 Mar 27 '18 at 17:13
  • Tried this method, but if i return "false" on this method the selection of cell is disabled. – subin272 Mar 28 '18 at 04:44
  • @dare: if the tableView is in `isEditing` mode, this function shouldn't be working... @subin272 - the selection events stop if you return false here, that is expected. – benc Jul 26 '23 at 16:33
0

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

vedhanish
  • 251
  • 3
  • 15
  • No i want the selection to happen, but doesn't want the cell to get highlighted. I want only the checkbox tick mark. – subin272 Mar 28 '18 at 04:47
0

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.

koen
  • 5,383
  • 7
  • 50
  • 89
  • A lot of new info has been added since I typed my answer yesterday. It is now more clear to me what you are looking for, and I don't think my answer is the right way forward for that. – koen Mar 28 '18 at 12:32
0

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
    }
Anton Novoselov
  • 769
  • 2
  • 7
  • 19
  • @anovoselov This is not working, in this case also the cell selection itself is getting disabled. – subin272 Mar 28 '18 at 04:57
  • @anovoselov Will check it out. – subin272 Mar 28 '18 at 08:24
  • @subin272 what did you do? – Daedelus Aug 03 '18 at 08:14
  • 2
    @Daedelus In default edit mode provided by table view we cannot change the highlighting color on selection. We have to implement edit mode programatically with custom cell. – subin272 Aug 03 '18 at 08:23
  • @subin272 if you use UIImageView as background, hightlighting does not bother you. i tried and succeed "finally". – Daedelus Aug 03 '18 at 13:03
  • @Daedelus even if we use image view..there will still be a slight shade right? So the issue of highlighting on selection wont go away i guess. Can you please share the code snippet. – subin272 Aug 03 '18 at 13:34
  • @subin272 There is no shade on image view. But there is a slight shade on background of cell. If you want to change this color you can override setSelected method of custom cell. This can provide you white color (not 'clearColor') like forwarding screen of WhatsApp application. Do you want me to share setSelected method? – Daedelus Aug 03 '18 at 13:44
  • @Daedelus I will check it. If possible share the code also here. – subin272 Aug 03 '18 at 14:51
  • @subin272 i figure out that setSelected method same as Harman's answer. https://stackoverflow.com/a/51089089/1517943 – Daedelus Aug 03 '18 at 14:58
  • @Daedelus okay i will check it. – subin272 Aug 03 '18 at 15:00