I have UItablview with custom cell i need to change a label background colour when select this row, but the label colour is repeated when scroll down
You could subclass your Cell like this (and cellForRow then will not be responsible for updating the color, only for setting default color).
class YourTableViewCellClass: UITableViewCell {
#IBOutlet weak var yourLabel: UILabel!
override func setSelected(_ selected: Bool, animated: Bool) {
if(selected) {
self.contentView.backgroundColor = UIColor.red //or what you want as your cell bg color
self.yourLabel.backgroundColor = UIColor.green //or what you want
} else {
self.contentView.backgroundColor = UIColor.white //or what you want as your cell bg color
self.yourLabel.backgroundColor = UIColor.red //or what you want
}
} }
What I understand is, you put code in didSelectRow method of tableview to change the color, but it shows previous color while scrolling.
So,you need to set condition in cellForRow method also e.g.
if(condition)
{
lbl.textcolor = x
}
else
{
lbl.textcolor = y
}
Related
I have given a tableview cell a color on selection in cellForRowAtIndexPath using
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.grey3 //custom color
cell.selectedBackgroundView = backgroundView
Since I am building with Xcode 11.0 the color is not propagated to the subviews of the cell anymore on an iOS 13 device or Simulator. If I build on an iOS 12.2 simulator using Xcode 11.0 it still works.
Anyone has an idea what has changed to cause this behaviour? I am working with .xib files.
From Apple's iOS 13 Release Notes:
The UITableViewCell class no longer changes the backgroundColor or isOpaque properties of the contentView and any of its subviews when cells become highlighted or selected. If you are setting an opaque backgroundColor on any subviews of the cell inside (and including) the contentView, the appearance when the cell becomes highlighted or selected might be affected. The simplest way to resolve any issues with your subviews is to ensure their backgroundColor is set to nil or clear, and their opaque property is false. However, if needed you can override the setHighlighted(:animated:) and setSelected(:animated:) methods to manually change these properties on your subviews when moving to or from the highlighted and selected states.
My quick test confirms this would be the cause in your case.
Cell with green-background label, orange view as .selectedBackgroundView.
iOS 12:
iOS 13:
If you use the hierarchy debugger, you'll see that in iOS 13 the contentView sits above the backgroundView and selectedBackgroundView.
This can be resolved by setting
contentView.backgroundColor = nil
in awakeFromNib
or setting the contentView's backgroundColour to clear in the storyboard
I had the same problem, my solutions is:
TableViewController:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testCell")! as! TestCell
// Turn off selection style for iOS12, iOS11, etc...
cell.selectionStyle = .none
return cell
}
Cell Class (I have a UIView inside cell's ContentView):
class TestCell: UITableViewCell {
#IBOutlet weak var testCellBackgroundView: UIView!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
contentView.backgroundColor = UIColor.white
testCellBackgroundView.backgroundColor = UIColor.red
} else {
contentView.backgroundColor = UIColor.white
testCellBackgroundView.backgroundColor = UIColor.green // default background color
}
}
// You may change highlighted color of a cell the same way
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
if highlighted {
contentView.backgroundColor = UIColor.white
testCellBackgroundView.backgroundColor = UIColor.red
} else {
contentView.backgroundColor = UIColor.white
testCellBackgroundView.backgroundColor = UIColor.green
}
}
}
Note: this is my first answer at stackoverflow, please check if it's correct.
I have a UITableView and have set multiple selection while editing to true
tableView.allowsMultipleSelectionDuringEditing = true
In the cell configuration (cellForRowAtIndexPath), I do:
cell.selectionStyle = .default
cell.accessoryType = .checkmark
So when tableView is set to editing mode, I get blue checkmark option on the left side. I want to change the default blue color on that accessory. How do I do that?
If you're using storyboard :
Change the tint color here
In your custom UITableViewCell, set cell's tintColor in awakeFromNib() like so,
class CustomCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.tintColor = .red
}
}
Just set the cell tint color whatever you want. It will automatically change the accessory type color.
[cell setTintColor:[UIColor whiteColor]];
enter image description here
a video clip of this UI, how does it look like
Please Help me how can I make one, I tried to build it by UITableView and for cell UIcollectionView it works well but I can't select collection-view Cell by code ( need after select change the cell view background color )
you can change the background color of UICollectionViewCell by overriding the cell property
override var isSelected: Bool {
didSet {
if isSelected {
self.contentView.backgroundColor = UIColor.blue
} else {
self.contentView.backgroundColor = UIColor.green
}
}
}
also since cell are reusable you may see some cells with blue color while scrolling as they are now reused, to avoid this override the method
override func prepareForReuse() {
super.prepareForReuse()
self.contentView.backgroundColor = UIColor.green
}
In my tableView I'm using custom cells with two custom labels. Eventually the text of these labels will change but at the same time, layoutIfNeeded() is called to animate some other things. This is causing the text in the labels to animate as well which I'm trying to prevent.
The code I'm using to change the text:
func tapped(recognizer: UIGestureRecognizer) {
let path = NSIndexPath(forRow: myData.count - 1, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(path) as! CustomCell
cell.infoLabel.text = "Changing text here"
UIView.animateWithDuration(0.5) { self.anotherView.layoutIfNeeded() }
}
To prevent the labels from animating I have tried adding this in CustomLabel and CustomCell, as well as in willDisplayCell:
override func layoutSubviews() {
UIView.performWithoutAnimation { super.layoutSubviews() }
}
Or using another way of preventing it to animate in CustomLabel and CustomCell:
override func layoutSubviews() {
CATransaction.begin()
CATransaction.setDisableActions(true)
super.layoutSubviews()
CATransaction.commit()
}
I've also tried a property observer setting the text and removing all animations at the same time:
var text: String {
didSet {
infoLabel.text = text
infoLabel.layer.removeAllAnimations()
}
}
Nothing seems to work unless I use:
CATransaction.begin()
CATransaction.setDisableActions(true)
cell.infoLabel.text = "Changing text here"
CATransaction.commit()
But then I'd have to use it everywhere I'm changing the text. Is there a place I've overlooked to do this more elegantly?
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()