Swift Change the tableviewcell border color according to data
Try to move your code into cellForRowAt
method like that
cell.layer.masksToBounds = true
cell.layer.cornerRadius = 5
cell.layer.borderWidth = 2
cell.layer.shadowOffset = CGSize(width: -1, height: 1)
let borderColor: UIColor = (stock[indexPath.row] == "inStock") ? .red : .green
cell.layer.borderColor = borderColor.cgColor
UITableViewCell how to add solid border with rounded corners
I'm not sure how to do it with CALayer(). I Always make a cell with 2 UIViews. 1 "background view" behind the other. This will also create a border. This will have the same result as far as I'm aware. Could this be what you're looking for?
class TableViewCell: UITableViewCell {
var viewBackground = UIView()
var viewBackgroundBorder = UIView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
backgroundColor = .clear
viewBackground.backgroundColor = UIColor.white
viewBackgroundBorder.backgroundColor = UIColor.red
viewBackgroundBorder.layer.cornerRadius = 10
viewBackground.layer.cornerRadius = 9
addSubview(viewBackgroundBorder)
addSubview(viewBackground)
viewBackgroundBorder.translatesAutoresizingMaskIntoConstraints = false
viewBackground.translatesAutoresizingMaskIntoConstraints = false
let constraints = [
viewBackgroundBorder.topAnchor.constraint(equalTo: topAnchor, constant: 0),
viewBackgroundBorder.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0),
viewBackgroundBorder.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0),
viewBackgroundBorder.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0),
viewBackground.topAnchor.constraint(equalTo: topAnchor, constant: 2),
viewBackground.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2),
viewBackground.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -2),
viewBackground.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -2)]
NSLayoutConstraint.activate(constraints)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
How to add border to custom UITableViewCell in ViewWillAppear
Try to move your logic to cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "IDENTIFIER", for: indexPath) as! CustomTableViewCell
// TODO: Cell logic
// Border logic
if secondCategory.currentSelection != nil {
cell.selectionStyle = UITableViewCell.SelectionStyle.none
cell.layer.borderWidth = 3.0
cell.layer.borderColor = UIColor.red.cgColor
}
return cell
}
Swift. Change Color of Custom Tableview Cell Label when selected
You just need to translate the same code to Swift.
var myCell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
myCell.customLabel.textColor = UIColor.redColor()
UITableView Cell selected Color?
I think you were on the right track, but according to the class definition for selectedBackgroundView
:
The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for section-group tables UITableViewStyleGrouped).
Therefore, if you're using a plain-style table, then you'll need to alloc-init a new UIView
having your desired background colour and then assign it to selectedBackgroundView
.
Alternatively, you could use:
cell.selectionStyle = UITableViewCellSelectionStyleGray;
if all you wanted was a gray background when the cell is selected. Hope this helps.
Related Topics
File Couldn't Be Opened Because You Don't Have Permission to View It Error
Swift Language Statically or Dynamically Dispatched
C-Style Uninitialized Pointer Passing in Apple Swift
Swift: Nstextfield Allow Only Specified Characters
Difference Between Optional Values in Swift
Using Nil-Coalescing Operator with Try? for Function That Throws and Returns Optional
Use of Nspathcontrol to Represent Virtual Path
Spritekit Skphysicsjointfixed Odd Behaviour
Compile Time Key Path Checking in Swift
How to Open Your App's Settings (Inside the Settings App) with Swift (iOS 11)
Why 'Self.Self' Compiles and Run in Swift
In Swift, Why Can't I Instantiate a Protocol When It Has an Initialiser
Formula to Pick Every Pixel in a Bitmap Without Repeating
How to Respond with an Image Using Vapor
Swift: Reduce Function with a Closure
Ar with iOS: Putting a Light in the Scene Makes Everything Black