Swift shapeLayer.fillColor not work in cell contentView
up vote
0
down vote
favorite
I have a chartView and a customized tableViewcell.
And I try to use chartView add on my tableviewcell.
This ViewController is child ViewController.
I push to this ViewController, and I want to call tapIndexAction(index: 0)
to hightlight my bar of index 0.
I also use "tableview didEndDisplaying" can't fill color.
What's wrong with me?
Thanks.
class ChartView: UIView {
let publishSub = PublishSubject<Int?>()
var isAlreadyInit = false
var chartInfo:ChartInfo?
init(info:ChartInfo?, frame:CGRect = CGRect.zero) {
self.chartInfo = info
super.init(frame:frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
if !self.isAlreadyInit {
if self.frame.width > 0 {
self.isAlreadyInit = true
self.drawChart()
}
}
}
func drawChart() {
if let tmpCharInfo = self.chartInfo {
let chartLayer = tmpChartInfo.getChartDrawLayer()
chartLayer.frame = self.bounds
chartLayer.drawChart()
self.layer.addSublayer(chartLayer)
}
}
func tapIndexAction(index: Int) {
if let chartLayer = (self.layer.sublayers?.last) as? ChartLayer {
chartLayer.tapIndex(index: index)
self.publishSub.onNext(index)
}
}
class ChartLayer: CALayer {
var drawInfo:ChartInfo
var touchFrame:[CGRect] = [CGRect]()
var drawLayers:[CAShapeLayer] = [CAShapeLayer]()
var tapIndex:Int?
init(info:ChartDrawInfo) {
self.drawInfo = info
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func tapIndex(index:Int) {
self.tapIndex = index
for idx in 0..<self.touchFrame.count {
let shapeLayer = self.drawLayers[idx]
if idx == index {
shapeLayer.fillColor = UIColor.red.cgColor //Not work in "tableview didEndDisplaying"
}else {
shapeLayer.fillColor = UIColor.blue.cgColor
}
}
}
}
class BarChartTableViewCell: UITableViewCell {
var barChartView: ChartView?
var chartInfo:ChartInfo?
func initCell(drawInfo: ChartInfo?) {
self.chartInfo = drawInfo
if self.barChartView != nil {
for view in self.contentView.subviews {
view.snp.removeConstraints()
view.removeFromSuperview()
}
}
self.barChartView = ChartView(drawInfo: self.chartInfo)
self.contentView.addSubview(barChartView)
barChartView?.snp.makeConstraints({ (make) in
make.left.equalTo(13)
make.top.equalTo(updateTimeLabel.snp.bottom)
make.right.equalToSuperview()
make.height.equalTo(202)
})
}
}
class ViewController: UITableViewDelegate, UITableViewDataSource {
let tableview = UITableView()
tableview.datasource = self
tableview.delegate = self
//numberOfRowInSection return 1
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if let chartCell = cell as? BarChartTableViewCell {
chartCell.barChartView?.tapIndexAction(index: 0)
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let barChartCell = tableView.dequeueReusableCell(withIdentifier: "BarChartTableViewCell", for: indexPath) as! BarChartTableViewCell
barChartCell.initCell(drawInfo: chartInfo)
return barChartCell
}
}
ios swift calayer layer cashapelayer
add a comment |
up vote
0
down vote
favorite
I have a chartView and a customized tableViewcell.
And I try to use chartView add on my tableviewcell.
This ViewController is child ViewController.
I push to this ViewController, and I want to call tapIndexAction(index: 0)
to hightlight my bar of index 0.
I also use "tableview didEndDisplaying" can't fill color.
What's wrong with me?
Thanks.
class ChartView: UIView {
let publishSub = PublishSubject<Int?>()
var isAlreadyInit = false
var chartInfo:ChartInfo?
init(info:ChartInfo?, frame:CGRect = CGRect.zero) {
self.chartInfo = info
super.init(frame:frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
if !self.isAlreadyInit {
if self.frame.width > 0 {
self.isAlreadyInit = true
self.drawChart()
}
}
}
func drawChart() {
if let tmpCharInfo = self.chartInfo {
let chartLayer = tmpChartInfo.getChartDrawLayer()
chartLayer.frame = self.bounds
chartLayer.drawChart()
self.layer.addSublayer(chartLayer)
}
}
func tapIndexAction(index: Int) {
if let chartLayer = (self.layer.sublayers?.last) as? ChartLayer {
chartLayer.tapIndex(index: index)
self.publishSub.onNext(index)
}
}
class ChartLayer: CALayer {
var drawInfo:ChartInfo
var touchFrame:[CGRect] = [CGRect]()
var drawLayers:[CAShapeLayer] = [CAShapeLayer]()
var tapIndex:Int?
init(info:ChartDrawInfo) {
self.drawInfo = info
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func tapIndex(index:Int) {
self.tapIndex = index
for idx in 0..<self.touchFrame.count {
let shapeLayer = self.drawLayers[idx]
if idx == index {
shapeLayer.fillColor = UIColor.red.cgColor //Not work in "tableview didEndDisplaying"
}else {
shapeLayer.fillColor = UIColor.blue.cgColor
}
}
}
}
class BarChartTableViewCell: UITableViewCell {
var barChartView: ChartView?
var chartInfo:ChartInfo?
func initCell(drawInfo: ChartInfo?) {
self.chartInfo = drawInfo
if self.barChartView != nil {
for view in self.contentView.subviews {
view.snp.removeConstraints()
view.removeFromSuperview()
}
}
self.barChartView = ChartView(drawInfo: self.chartInfo)
self.contentView.addSubview(barChartView)
barChartView?.snp.makeConstraints({ (make) in
make.left.equalTo(13)
make.top.equalTo(updateTimeLabel.snp.bottom)
make.right.equalToSuperview()
make.height.equalTo(202)
})
}
}
class ViewController: UITableViewDelegate, UITableViewDataSource {
let tableview = UITableView()
tableview.datasource = self
tableview.delegate = self
//numberOfRowInSection return 1
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if let chartCell = cell as? BarChartTableViewCell {
chartCell.barChartView?.tapIndexAction(index: 0)
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let barChartCell = tableView.dequeueReusableCell(withIdentifier: "BarChartTableViewCell", for: indexPath) as! BarChartTableViewCell
barChartCell.initCell(drawInfo: chartInfo)
return barChartCell
}
}
ios swift calayer layer cashapelayer
tableView(_:didEndDisplaying:forRowAt:)
tells the delegate that the specified cell was removed from the table. You want one of the selectRow actions, e.g.tableView(_:willSelectRowAt:)
ortableView(_:didSelectRowAt:)
.
– JonJ
Nov 7 at 15:53
@JonJ this viewcontroller is child viewcontroller not parent. i don't need to selectRow.
– JimmyLee
Nov 7 at 16:00
If the view controller is a child and NOT a tableViewController, then it won't respond to delegate functions. You should dequeue the cell in the tableViewController. Its unclear what you're trying to do.
– JonJ
Nov 7 at 16:11
@JonJ it's a viewcontroller with tableview. I update the question. I trying to init the cell and call function "tapIndex" to make it's layer filled color. And layer.fillcolor not work. I want to know why. That's all.
– JimmyLee
Nov 7 at 16:25
See first comment - your cell is never removed from the table (as there's only one) sofunc didEndDisplaying
is never called, so tapIndexAction isn't called. Add a tap action to the cell itself to then call the func to determine the index.
– JonJ
Nov 7 at 16:43
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a chartView and a customized tableViewcell.
And I try to use chartView add on my tableviewcell.
This ViewController is child ViewController.
I push to this ViewController, and I want to call tapIndexAction(index: 0)
to hightlight my bar of index 0.
I also use "tableview didEndDisplaying" can't fill color.
What's wrong with me?
Thanks.
class ChartView: UIView {
let publishSub = PublishSubject<Int?>()
var isAlreadyInit = false
var chartInfo:ChartInfo?
init(info:ChartInfo?, frame:CGRect = CGRect.zero) {
self.chartInfo = info
super.init(frame:frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
if !self.isAlreadyInit {
if self.frame.width > 0 {
self.isAlreadyInit = true
self.drawChart()
}
}
}
func drawChart() {
if let tmpCharInfo = self.chartInfo {
let chartLayer = tmpChartInfo.getChartDrawLayer()
chartLayer.frame = self.bounds
chartLayer.drawChart()
self.layer.addSublayer(chartLayer)
}
}
func tapIndexAction(index: Int) {
if let chartLayer = (self.layer.sublayers?.last) as? ChartLayer {
chartLayer.tapIndex(index: index)
self.publishSub.onNext(index)
}
}
class ChartLayer: CALayer {
var drawInfo:ChartInfo
var touchFrame:[CGRect] = [CGRect]()
var drawLayers:[CAShapeLayer] = [CAShapeLayer]()
var tapIndex:Int?
init(info:ChartDrawInfo) {
self.drawInfo = info
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func tapIndex(index:Int) {
self.tapIndex = index
for idx in 0..<self.touchFrame.count {
let shapeLayer = self.drawLayers[idx]
if idx == index {
shapeLayer.fillColor = UIColor.red.cgColor //Not work in "tableview didEndDisplaying"
}else {
shapeLayer.fillColor = UIColor.blue.cgColor
}
}
}
}
class BarChartTableViewCell: UITableViewCell {
var barChartView: ChartView?
var chartInfo:ChartInfo?
func initCell(drawInfo: ChartInfo?) {
self.chartInfo = drawInfo
if self.barChartView != nil {
for view in self.contentView.subviews {
view.snp.removeConstraints()
view.removeFromSuperview()
}
}
self.barChartView = ChartView(drawInfo: self.chartInfo)
self.contentView.addSubview(barChartView)
barChartView?.snp.makeConstraints({ (make) in
make.left.equalTo(13)
make.top.equalTo(updateTimeLabel.snp.bottom)
make.right.equalToSuperview()
make.height.equalTo(202)
})
}
}
class ViewController: UITableViewDelegate, UITableViewDataSource {
let tableview = UITableView()
tableview.datasource = self
tableview.delegate = self
//numberOfRowInSection return 1
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if let chartCell = cell as? BarChartTableViewCell {
chartCell.barChartView?.tapIndexAction(index: 0)
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let barChartCell = tableView.dequeueReusableCell(withIdentifier: "BarChartTableViewCell", for: indexPath) as! BarChartTableViewCell
barChartCell.initCell(drawInfo: chartInfo)
return barChartCell
}
}
ios swift calayer layer cashapelayer
I have a chartView and a customized tableViewcell.
And I try to use chartView add on my tableviewcell.
This ViewController is child ViewController.
I push to this ViewController, and I want to call tapIndexAction(index: 0)
to hightlight my bar of index 0.
I also use "tableview didEndDisplaying" can't fill color.
What's wrong with me?
Thanks.
class ChartView: UIView {
let publishSub = PublishSubject<Int?>()
var isAlreadyInit = false
var chartInfo:ChartInfo?
init(info:ChartInfo?, frame:CGRect = CGRect.zero) {
self.chartInfo = info
super.init(frame:frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
if !self.isAlreadyInit {
if self.frame.width > 0 {
self.isAlreadyInit = true
self.drawChart()
}
}
}
func drawChart() {
if let tmpCharInfo = self.chartInfo {
let chartLayer = tmpChartInfo.getChartDrawLayer()
chartLayer.frame = self.bounds
chartLayer.drawChart()
self.layer.addSublayer(chartLayer)
}
}
func tapIndexAction(index: Int) {
if let chartLayer = (self.layer.sublayers?.last) as? ChartLayer {
chartLayer.tapIndex(index: index)
self.publishSub.onNext(index)
}
}
class ChartLayer: CALayer {
var drawInfo:ChartInfo
var touchFrame:[CGRect] = [CGRect]()
var drawLayers:[CAShapeLayer] = [CAShapeLayer]()
var tapIndex:Int?
init(info:ChartDrawInfo) {
self.drawInfo = info
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func tapIndex(index:Int) {
self.tapIndex = index
for idx in 0..<self.touchFrame.count {
let shapeLayer = self.drawLayers[idx]
if idx == index {
shapeLayer.fillColor = UIColor.red.cgColor //Not work in "tableview didEndDisplaying"
}else {
shapeLayer.fillColor = UIColor.blue.cgColor
}
}
}
}
class BarChartTableViewCell: UITableViewCell {
var barChartView: ChartView?
var chartInfo:ChartInfo?
func initCell(drawInfo: ChartInfo?) {
self.chartInfo = drawInfo
if self.barChartView != nil {
for view in self.contentView.subviews {
view.snp.removeConstraints()
view.removeFromSuperview()
}
}
self.barChartView = ChartView(drawInfo: self.chartInfo)
self.contentView.addSubview(barChartView)
barChartView?.snp.makeConstraints({ (make) in
make.left.equalTo(13)
make.top.equalTo(updateTimeLabel.snp.bottom)
make.right.equalToSuperview()
make.height.equalTo(202)
})
}
}
class ViewController: UITableViewDelegate, UITableViewDataSource {
let tableview = UITableView()
tableview.datasource = self
tableview.delegate = self
//numberOfRowInSection return 1
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if let chartCell = cell as? BarChartTableViewCell {
chartCell.barChartView?.tapIndexAction(index: 0)
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let barChartCell = tableView.dequeueReusableCell(withIdentifier: "BarChartTableViewCell", for: indexPath) as! BarChartTableViewCell
barChartCell.initCell(drawInfo: chartInfo)
return barChartCell
}
}
ios swift calayer layer cashapelayer
ios swift calayer layer cashapelayer
edited Nov 7 at 16:22
asked Nov 7 at 15:45
JimmyLee
6811
6811
tableView(_:didEndDisplaying:forRowAt:)
tells the delegate that the specified cell was removed from the table. You want one of the selectRow actions, e.g.tableView(_:willSelectRowAt:)
ortableView(_:didSelectRowAt:)
.
– JonJ
Nov 7 at 15:53
@JonJ this viewcontroller is child viewcontroller not parent. i don't need to selectRow.
– JimmyLee
Nov 7 at 16:00
If the view controller is a child and NOT a tableViewController, then it won't respond to delegate functions. You should dequeue the cell in the tableViewController. Its unclear what you're trying to do.
– JonJ
Nov 7 at 16:11
@JonJ it's a viewcontroller with tableview. I update the question. I trying to init the cell and call function "tapIndex" to make it's layer filled color. And layer.fillcolor not work. I want to know why. That's all.
– JimmyLee
Nov 7 at 16:25
See first comment - your cell is never removed from the table (as there's only one) sofunc didEndDisplaying
is never called, so tapIndexAction isn't called. Add a tap action to the cell itself to then call the func to determine the index.
– JonJ
Nov 7 at 16:43
add a comment |
tableView(_:didEndDisplaying:forRowAt:)
tells the delegate that the specified cell was removed from the table. You want one of the selectRow actions, e.g.tableView(_:willSelectRowAt:)
ortableView(_:didSelectRowAt:)
.
– JonJ
Nov 7 at 15:53
@JonJ this viewcontroller is child viewcontroller not parent. i don't need to selectRow.
– JimmyLee
Nov 7 at 16:00
If the view controller is a child and NOT a tableViewController, then it won't respond to delegate functions. You should dequeue the cell in the tableViewController. Its unclear what you're trying to do.
– JonJ
Nov 7 at 16:11
@JonJ it's a viewcontroller with tableview. I update the question. I trying to init the cell and call function "tapIndex" to make it's layer filled color. And layer.fillcolor not work. I want to know why. That's all.
– JimmyLee
Nov 7 at 16:25
See first comment - your cell is never removed from the table (as there's only one) sofunc didEndDisplaying
is never called, so tapIndexAction isn't called. Add a tap action to the cell itself to then call the func to determine the index.
– JonJ
Nov 7 at 16:43
tableView(_:didEndDisplaying:forRowAt:)
tells the delegate that the specified cell was removed from the table. You want one of the selectRow actions, e.g. tableView(_:willSelectRowAt:)
or tableView(_:didSelectRowAt:)
.– JonJ
Nov 7 at 15:53
tableView(_:didEndDisplaying:forRowAt:)
tells the delegate that the specified cell was removed from the table. You want one of the selectRow actions, e.g. tableView(_:willSelectRowAt:)
or tableView(_:didSelectRowAt:)
.– JonJ
Nov 7 at 15:53
@JonJ this viewcontroller is child viewcontroller not parent. i don't need to selectRow.
– JimmyLee
Nov 7 at 16:00
@JonJ this viewcontroller is child viewcontroller not parent. i don't need to selectRow.
– JimmyLee
Nov 7 at 16:00
If the view controller is a child and NOT a tableViewController, then it won't respond to delegate functions. You should dequeue the cell in the tableViewController. Its unclear what you're trying to do.
– JonJ
Nov 7 at 16:11
If the view controller is a child and NOT a tableViewController, then it won't respond to delegate functions. You should dequeue the cell in the tableViewController. Its unclear what you're trying to do.
– JonJ
Nov 7 at 16:11
@JonJ it's a viewcontroller with tableview. I update the question. I trying to init the cell and call function "tapIndex" to make it's layer filled color. And layer.fillcolor not work. I want to know why. That's all.
– JimmyLee
Nov 7 at 16:25
@JonJ it's a viewcontroller with tableview. I update the question. I trying to init the cell and call function "tapIndex" to make it's layer filled color. And layer.fillcolor not work. I want to know why. That's all.
– JimmyLee
Nov 7 at 16:25
See first comment - your cell is never removed from the table (as there's only one) so
func didEndDisplaying
is never called, so tapIndexAction isn't called. Add a tap action to the cell itself to then call the func to determine the index.– JonJ
Nov 7 at 16:43
See first comment - your cell is never removed from the table (as there's only one) so
func didEndDisplaying
is never called, so tapIndexAction isn't called. Add a tap action to the cell itself to then call the func to determine the index.– JonJ
Nov 7 at 16:43
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192887%2fswift-shapelayer-fillcolor-not-work-in-cell-contentview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
tableView(_:didEndDisplaying:forRowAt:)
tells the delegate that the specified cell was removed from the table. You want one of the selectRow actions, e.g.tableView(_:willSelectRowAt:)
ortableView(_:didSelectRowAt:)
.– JonJ
Nov 7 at 15:53
@JonJ this viewcontroller is child viewcontroller not parent. i don't need to selectRow.
– JimmyLee
Nov 7 at 16:00
If the view controller is a child and NOT a tableViewController, then it won't respond to delegate functions. You should dequeue the cell in the tableViewController. Its unclear what you're trying to do.
– JonJ
Nov 7 at 16:11
@JonJ it's a viewcontroller with tableview. I update the question. I trying to init the cell and call function "tapIndex" to make it's layer filled color. And layer.fillcolor not work. I want to know why. That's all.
– JimmyLee
Nov 7 at 16:25
See first comment - your cell is never removed from the table (as there's only one) so
func didEndDisplaying
is never called, so tapIndexAction isn't called. Add a tap action to the cell itself to then call the func to determine the index.– JonJ
Nov 7 at 16:43