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

}


}









share|improve this question
























  • 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












  • 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















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

}


}









share|improve this question
























  • 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












  • 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













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

}


}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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:) 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












  • 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


















  • 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












  • 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
















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()