Swift shapeLayer.fillColor在单元格contentView中不起作用

时间:2018-11-07 15:45:37

标签: ios swift calayer layer cashapelayer

我有一个chartView和一个自定义的tableViewcell。
我尝试在tableviewcell上使用chartView添加。
该ViewController是子ViewController。
我推送到此ViewController,并想调用tapIndexAction(index: 0)以突出显示索引0的栏。
我还使用“ tableview didEndDisplaying”无法填充颜色。
我怎么了?
谢谢

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

    }


}

0 个答案:

没有答案