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

String to NSManagedObjectID
How to convert a byte to a String in Swift?
Set delegate of UIGestureRecorgniser to parent
Going from one ViewController to another ViewController and updating
Setting Text View to be Int rather than String? Swift
Why I am getting ERROR: Type 'Any' has no subscript members when trying to use ELCimagepickercontroller
UIActivityViewController using multiple arrays
how to call Swift closure callback
Realm Notification is not triggered when NSPredicate changes
Parse can't retrieve CurrentUser's array objects
Use of unresolved type MutableProperty
How to have label text change to message after user types in textfield?
RxSwift/RxCocoa: Code Completion barely working
Completion block never called at end of SKAction sequence of groups
What is the best way to zoom and deplace nodes?
How to handle PUT HTTP request in Vapor?

Categories

HOME
checkbox
angular-cli
ubuntu-16.04
vs-team-services
json-ld
thunderbird-addon
visualization
specflow
aruco
height
x11
ctypes
cosmicmind
equalizer
eclipse-luna
internet-explorer-8
propel2
ip-camera
spring-test
nmf
activepython
net-snmp
rhmap
spring-data-neo4j
cgal
pari
clockwork
spring-profiles
stocks
honeysql
pox
nunit-3.0
inkscape
zero
declare
agent
soundjs
exiftool
stdclass
nsexception
code-composer
bing-translator-api
catia
python-idle
chrome-remote-desktop
cdk
date-range
cleditor
update-site
nssplitview
selecteditem
search-regex
financial
eml
handlebars.java
throughput
site-prism
apache-mina
scrollspy
mdm-zinc
subversion-edge
low-level
java-melody
tilestache
sysctl
alphablending
ghostdoc
level
pic24
gfs
jquery-tabs
modeshape
google-closure-library
grunt-contrib-compass
mvs
pys60
crocodoc
autostart
vim-powerline
cadisplaylink
scrollto
visual-web-gui
webkit-transform
search-path
nvelocity
symbol-server
requestfactory
gwt-2.2-celltable
google-instant
asp.net-mvc-controller
web-statistics

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