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
Related
I use the following to set the selected backgroud color of my UITableViewCells:
let backgroundColorView = UIView()
backgroundColorView.backgroundColor = .red
UITableViewCell.appearance().selectedBackgroundView = backgroundColorView
When I tap to select them, the cell briefly flashes red and then return to the backgroundColor I've set.
When I add:
if cell.isSelected
{
cell.backgroundColor = .red
}
to cellForRowAt indexPath, the cell becomes red when I scroll it out of sight and back again.
How can I make sure that the cell remains red immediately after I tap it?
EDIT:
If I add the following code to cellForRowAt, things work as expected:
let backgroundColorView = UIView()
backgroundColorView.backgroundColor = .red
cell.selectedBackgroundView = backgroundColorView
Could be an iOS bug?
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
How do I set a transparent background for the sections index bar in a UITableView?
I've tried:
tableView.sectionIndexBackgroundColor = .clearColor()
but the bar gets a light grey background
EDIT
I've tried to change background color of the tableview but it doesn't seem to influence.
tableView.backgroundView = UIView.init()
tableView.backgroundColor = .blackColor() // tried .clearColor() too.
EDIT2
tableView.sectionIndexBackgroundColor = UIColor(white: CGFloat(1.0), alpha: CGFloat(0.5)) // change the alpha and check once
I've also tried a color with alpha values. I can make the background semi-transparent (see line code above). But if I set alpha to 0.0 to make it completely transparent, I get again the light grey background.
tableView.sectionIndexBackgroundColor = .clear works perfect
For swift 4.2 you must set viewBackground for your section and for that view set background color.
class CommonSection: UITableViewHeaderFooterView { static let
lightSectionColor = UIColor.gray.withAlphaComponent(0.005)
func setup(title: String, sectionColor: UIColor? = nil) {
textLabel?.text = title
textLabel?.font = UIFont.preferredFont(forTextStyle: .largeTitle)
guard let color = sectionColor else { return }
self.backgroundView = Init(value: UIView()) {
$0.backgroundColor = color
} } }
Note: Init just shortcut for initializing elements.
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
}()
I have a tableview in a view controller that is dynamically populated with data from a database. Now I have set the tableview to be clear and it working correctly, but I have tried to set the cells to be clear to no avail ?
cell.backgroundColor = UIColor.clearColor()
That line has been placed in the cellForRowAtIndexPath function.
This little extension should help you.
The idea is to set the backgorundView to clear too and not just the backgroundColor:
extension UITableViewCell {
func setTransparent() {
let bgView: UIView = UIView()
bgView.backgroundColor = .clearColor()
self.backgroundView = bgView
self.backgroundColor = .clearColor()
}
}
Usage:
In the cellForRowAtIndexPath add the following line:
cell.setTransparent()