0

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

    }


}
JimmyLee
  • 507
  • 2
  • 7
  • 24
  • `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 07 '18 at 15:53
  • @JonJ this viewcontroller is child viewcontroller not parent. i don't need to selectRow. – JimmyLee Nov 07 '18 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 07 '18 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 07 '18 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 07 '18 at 16:43

0 Answers0