I am working on a UITableView with customized cells from a .xib. I have added a blue border to the top and bottom of the .xib to give a customized spacing between the cells. I don't want a separator between the cells so I set the Separator to None in the storyboard. This is the look that it gives me.
This works great until I select a cell, at which point I am seeing this (notice the white separators above and below the selected cell):
I am using this code in the cellForRowAt function to set the color of the selected cell to white, but this also seems to force the separator to be white:
let selectedView = UIView()
selectedView.backgroundColor = .white
cell.selectedBackgroundView = selectedView
I am trying to figure out how to remove the separator lines when the cell is selected. I have tried several things on the storyboard and in code, but haven't found anything that works.For example:
I can't use cell.selectionStyle = .none because I want the cell background to turn white when selected.
This doesn't change anything when called from viewDidAppear: tableView.separatorColor = UIColor.clear
I tried the answer here Hide separator line on one UITableViewCell by Avinash but it didn't do anything.
Any ideas?
I found that this solution worked for me.
I changed my code in the cellForRowAt function to the clear color:
let selectedView = UIView()
selectedView.backgroundColor = .clear
cell.selectedBackgroundView = selectedView
try change the tableView.separatorColor = tableView.backgroundColor
and set you table style to grouped
Related
In my tableView, I have a subView with a background colour of red and anytime I select a cell in the tableview the background colour disappears. I was wondering if there was a way to fix this?
You have to stop tableview cell selection style. Write below code in cellForRowAtIndexPath method.
cell.selectionStyle = .none
For set cell background color on selection.
cell.contentView.backgroundColor = UIColor.red // here you can set your own custom color.
Try below code.
cell.selectionstyle = .none
This is very straightforward. I've tried this a couple of times, in both tableView:willDisplayCell:forRowAtIndexPath: and tableView:CellForRow:atIndexPath:.
Here is my code:
cell.alpha = 0.0
cell.backgroundView?.alpha = 0.0
cell.contentView.alpha = 0.0
cell.backgroundView?.backgroundColor = UIColor.clear
cell.backgroundColor = UIColor.clear
cell.contentView.backgroundColor = UIColor.clear
I'm pretty sure this should be overkill, but I'm having a really hard time making this work. The cells are being modified inside an if statement because this only applies to certain cells. Yes, I've checked, and the if statement is working as expected. Any ideas?
I am using Xcode version 8.2 beta
You should set:
cell.backgroundColor = .clear
and also you should provide clear color for whole tableView:
tableView.backgroundColor = .clear
I've tested in sample project. I create UIViewController with UITableView. Set the .red color for view of UIViewController. Create UITableView, apply .clear color for cell and tableView. You should see the red colored background - so the cells and tableView are transparent.
Here are example screen(The label is added to .view of UIViewController to make sure that tableView is transparent):
As a UITableView is allowsMultipleSelectionDuringEditing = true,
the default selection style is a light-blue look on a selected cell:
I set the cell contentView.backgroundColor = .whiteColor() when the cell got highlighted/ selected,
consequently make the circle-checkmark area remain light-blue but not the whole cell:
TL;DR
I need the whole cell to be white as it's multiple-selected,
which means I cannot set the cell's selectionStyle = .None.
Is there a way to achieve this?
Have you tried cell.backgroundColor = UIColor.whiteColor() ?
UITableView in multi-selection mode removes background colours of all subviews in selected cell. I want not to change the backgrounds colours of my subviews just show the tick mark on selection. I tried to assign cell's "selectedBackgroundView" and "multipleSelectionBackgroundView" to a transparent view but it didn't work. I also tried to reassign the backgrounds of my subviews in "setEditing" and "setHighlighted" functions of cell but it also didn't work. Is there any other way to fix this issue?
Set the tableView cell Selection to None in the Attributes inspector, or set cell.selectionStyle = .None in code.
for Swift 3
cell.selectionStyle = .gray
cell.selectedBackgroundView = {
let colorView = UIView()
colorView.backgroundColor = UIColor.clear
return colorView
}()
When I tap or hold one of the cells in the UITableView the color of the cell becomes gray by default. I want it to be some other color. I looked everywhere in the storyboard and on stack overflow but could't find it. How do we do it.
Try to change it like this, which will be red for example:
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.redColor()
cell.selectedBackgroundView = bgColorView
This is an extension based to #evgeny-karkan answer
extension UITableViewCell {
func ChangePressDownColor(to Color: UIColor){
let bgColorView = UIView()
bgColorView.backgroundColor = Color
self.selectedBackgroundView = bgColorView
}
}
Usage:
cell.ChangePressDownColor(to: .red)
check the selection after selecting the cell, where it currently says default.
however, this is very limited number of selection.
if you want custom, you should do it programatically.
i.e./ cell.selectedBackgroundView = customView with color