Disable tableview cell highlighted color in edit mode?
Asked Answered
O

7

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.

Oina answered 27/3, 2018 at 17:3 Comment(9)
Try cell?.selectionStyle = .none in cellForRowAtIndexPath or you can disable the selection of cell from storyboard too.Urfa
@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.Oina
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 didSelectRowAtIndexPathUrfa
@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.Oina
Can share the existing delegate-datasource methods?Urfa
@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.Oina
@Oina : It is not possible with "AllowsMultipleSelectionDuringEditing" you have to design custom cell with edit radio button.Booted
Possible duplicate of disable the uitableview highlighting but allow the selection of individual cellsSori
@Koen it's not duplicate. This question differs from yours with 'edit mode'.Stately
U
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")
}
Urfa answered 28/3, 2018 at 5:38 Comment(0)
N
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
    }
}
Nannettenanni answered 28/6, 2018 at 18:10 Comment(1)
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) }Phosphorylase
H
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://mcmap.net/q/333155/-uitableview-separator-line-disappears-when-selecting-cells-in-ios7

Hylton answered 28/10, 2020 at 23:14 Comment(0)
D
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.

Disapprove answered 27/3, 2018 at 17:10 Comment(3)
Will check it out. Thank you.Oina
Tried this method, but if i return "false" on this method the selection of cell is disabled.Oina
@dare: if the tableView is in isEditing mode, this function shouldn't be working... @Oina - the selection events stop if you return false here, that is expected.Ruling
A
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

Aideaidedecamp answered 27/3, 2018 at 17:27 Comment(1)
No i want the selection to happen, but doesn't want the cell to get highlighted. I want only the checkbox tick mark.Oina
S
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.

Sori answered 27/3, 2018 at 17:42 Comment(1)
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.Sori
E
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
    }
Ego answered 27/3, 2018 at 18:42 Comment(10)
@anovoselov This is not working, in this case also the cell selection itself is getting disabled.Oina
@anovoselov Will check it out.Oina
@Oina what did you do?Stately
@Stately 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.Oina
@Oina if you use UIImageView as background, hightlighting does not bother you. i tried and succeed "finally".Stately
@Stately 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.Oina
@Oina 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?Stately
@Stately I will check it. If possible share the code also here.Oina
@Oina i figure out that setSelected method same as Harman's answer. https://mcmap.net/q/1488353/-disable-tableview-cell-highlighted-color-in-edit-modeStately
@Stately okay i will check it.Oina

© 2022 - 2024 — McMap. All rights reserved.