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

Swift: Error -1001 when calculating route
Custom navigation bar in Swift 3
How to filter objects by string comparison in Swift
Date Picker Only Works After Clicking out of Text Field
optional variable in class definition
Swift UILabel word wrap for Chinese?
Not able to get results for spotlight search for user tags with NSMetadataquery and kMDItemUserTags
swift: how to generate arm7 or arm64 from the command line
Dictionary<string,Dictionary> in swift 3.0 giving nil when tried to add a dict [duplicate]
Unwrap optional inside switch case [duplicate]
why is my xib button not aligning with my simulator keyboard screen?
Xcode swift login with Facebook SDK app not loading
Get next value on a map?
Swift Detecting a Change in a Textfield from a .xib file
Firebase retrieve data multiple filters [duplicate]
Swift3 setting constraints relative to parent view of parent view

Categories

HOME
openssl
voip
bing
visual-studio-2013
thunderbird-addon
actionscript
cocos2d-x-3.0
automated-tests
is-empty
spring-cloud-contract
docker-windows
internet-explorer-11
actionscript-2
octopus-deploy
oclint
flexbox
wicket
cakephp-2.9
public-key-encryption
spring-cloud-config
cross-platform
jcl
flexlm
jni
backpropagation
rworldmap
restfb
searchbar
lync-2013
edge-detection
getjson
crt
twitter-bootstrap-2
protobuf-net
read-write
body-parser
superpowered
viewstate
image-compression
mongoexport
duktape
fltk
s3cmd
dart-pub
distributed-transactions
excel-interop
homekit
lftp
powermta
upsert
rapidweaver
recycle-bin
istorage
jenkins-jira-trigger
chrome-remote-desktop
etherpad
httplib2
launch
paas
opencpu
onresume
build-process
cleditor
update-site
dts
date-format
inet
geodjango
android-snackbar
python-winshell
achartengine
rspec3
pylearn
xaml-designer
shift-jis
sysfs
consensus
ibmsbt
dmp
firebug-lite
emma
late-static-binding
cdata
installshield-2011
attachevent
legacy-code
gdt
zookeeper
authenticode
libavformat
getresource
msn
hardware-acceleration
visual-c++-2008-express
microsoft.ink
winsnmp
ajaxpro
interface-design
document-library

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