swift


Cell componenent gradient layer does not display while UITableView loaded


I am using gradients for my top view which is just a border. The border have different colors, like whenever it is a single color then i set the gradient by gradient.backgroundColor = firstColor.cgColor, whenver it has 2 colors then i do gradient.colors = [secondColor.cgColor, firstColor.cgColor].
The problem is that whenever I load screen for a first time, the layers does not display. Only whenver I scroll down and back I can see them displayed.
Here is a code :
The function which is taking care of recognizing which color is it and how many of them is :
func setTopLineColor(myModel: MyModel, colorView: UIView) -> CAGradientLayer {
var colors: [String] = []
colors = myModel.color.components(separatedBy: "_")
let gradient = CAGradientLayer()
gradient.frame = colorView.frame
gradient.cornerRadius = 4
print(colors)
switch colors.count {
case 1:
let firstColor = UIColor.init().toUIColor(forName: colors[0])
gradient.backgroundColor = firstColor.cgColor
return gradient
case 2:
let firstColor = UIColor.init().toUIColor(forName: colors[0])
let secondColor = UIColor.init().toUIColor(forName: colors[1])
if colors[0] == "white" {
gradient.colors = [secondColor.cgColor, firstColor.cgColor]
} else {
gradient.colors = [firstColor.cgColor, secondColor.cgColor]
}
return gradient
default:
return CAGradientLayer()
}
}
Then in UITableViewDataSource :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyCell
let topColorLayer = myModel.setTopLineColor(myModel: myModel, colorView: cell.topColorView)
cell.topColorView.layer.addSublayer(topColorLayer)
return cell
}
Registering cell :
myTableView.register(MyCell.self, forCellReuseIdentifier: "myCell")
The last thing is that MyCell class have overridden prepareForReuse() with self.topColorView.backgroundColor = nil.
Does any one know what am I missing ? Or what might be wrong ?
Thanks in advance!

Related Links

Why doesn't work Authorization Services?
Cannot load default library in Metal using Swift
Issue with completion handlers in Swift
Custom UITableViewCell UIButton setTitle not working as planned
FloatingPoint in place of Float/CGFloat?
'Incorrect argument label' when method overloading in Swift
Understanding the “last cached state” of NSManagedObject
Swift Perfect does not load image or css
Generic class with reference to itself
Index of enum element in for-case pattern matching
Get Size of UImage when in UIImageView when ContentMode = AspectFill
Last index of Collection as a new Image for Annotation
Cannot convert value of type Int to expected argument type IndexPath (Core Data)?
Disable automatic convenience initializer inheritance in Swift
Getting crash while adding a new node in firebase using swift 3
Protocol function implementation without actually conforming to a protocol

Categories

HOME
rotation
google-contacts
angular2-directives
console
snap.svg
urlencode
uiactivityviewcontroller
metronic
jboss-eap-7
finite-group-theory
jacoco
typeahead
titan
apache-karaf
flexlm
aws-cognito
thingsboard
qsub
fabric
pipelinedb
rhmap
excel-2013
pentaho-report-designer
frp
partial-application
red-black-tree
spring-profiles
exponential
construct-2
shibboleth
nlb
ksoap
poco-libraries
image-compression
mod-fcgid
jtds
sgmlreader
jgraph
ogre3d
crash-reports
date-range
efxclipse
svn-merge
coremidi
backstop.js
rstudio-server
git-annex
com-interop
achievements
faraday
mongo-c-driver
servlet-3.0
datainputstream
enyo
vips
uialertview
retro-computing
rx-groovy
virus
sysfs
chaining
temp-tables
ultrawingrid
x-ua-compatible
node-imagemagick
funq
planetary.js
zipalign
logentries
bignum
osx-snow-leopard
goinstant
trimming
jqmobi
vmware-server
client-side-scripting
iconv
mvccontrib
static-variables
responsetext
qtembedded
gtk2hs
.app
libavformat
task-management
file-encodings
communicationexception

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App