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?
Related
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
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
I tried to change the background and separator style in a UITableView, but nothing happens. In the snippet below, everything besides those changes works.
This tableView is located in a #IBDesignable UIView. Best part is, that everything shows as it should in the interface builder. The colors change, the separators dissapear.
func getTableViewToDisplayRecords() -> UITableView {
let tableView:UITableView = UITableView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.frame.size.width, height: (self.frame.size.height * 3) / 4)), style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView(frame:CGRectZero)
tableView.backgroundColor = UIColor.clearColor()
tableView.separatorStyle = .None
return tableView
}
What could be causing this behavior?
----- EDIT -----
I have managed to do a workaround:
UITableView.appearance().backgroundColor = UIColor.clearColor()
UITableView.appearance().separatorStyle = .None
Now everything works fine, although this is not really a solution of the problem. Any new ideas anyone?
Try the code snippet below to change the background colour of UITableview along with no separator between the table cell's.
{
tableView.backgroundColor = UIColor.clearColor()
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
}
Also, you need to change the background color of UITableViewCells
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
// change the background color of cell, which is by-default white
cell.backgroundColor = UIColor.clearColor()
return cell
}
Here is the screenshot with the table background color being red (for demonstration purpose) and no seperator between cell.
And here is another image with table background being red and single line seperator between cell.
Hope this helps!!
I think this is a Bug in UITableView.
When your tableView move to superview, sometimes the 'separatorStyle' property will reset to UITableViewCellSeparatorStyleSingleLine(the default style).
So you must set this property again when you finish frame change. Like this:
[self.view addSubview:_searchContentView];
_searchContentView.separatorStyle = UITableViewCellSeparatorStyleNone;
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()