gpt4 book ai didi

swift - iosChart 不在 UITableViewCell 中显示 LineChartView 点

转载 作者:行者123 更新时间:2023-11-28 05:38:42 24 4
gpt4 key购买 nike

以下基于 LineChartView 示例 ( from youtube ),它在标准 View (单 View 应用程序)中运行良好,而 BarChartViewTableViewCell 使用与下面所示相同的习惯用法。我有一个每两秒到期的时间来更新单元格,但尚未连接。

x,y 轴生成正确(最终图形仅显示 9 个单元格中的 2 个),只是没有绘制点。但是,失败仅发生在 LineChartView 中。我查看了有关此问题的其他引用资料,但尚未找到我的具体修复方法。有什么想法吗?

TableViewController

class GraphTableTableViewController: UITableViewController {

let model = [1,2,3,4,5,6,7,8,9]
var timer: Timer? = nil
var noSamples = 0

override func viewDidLoad() {
super.viewDidLoad()

timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "lineChart_cell", for: indexPath)
let graphCell = cell as? LineGraphCell

graphCell!.setChartValues()
print("\(model[indexPath.row])")
return graphCell!
}

@objc func fireTimer() {
print("Timer Fired")
noSamples += 1
(view as? UITableView)?.reloadData()
}

}

TableViewCell 实现如下所示:

class LineGraphCell: UITableViewCell {

@IBOutlet weak var lineChart: LineChartView!

let x = [0,1,2,3,4,5,6,7,8,9]
let y = [0,1,2,3,4,5,6,7,8,9]

func setChartValues() {

var values = [ChartDataEntry]()
for index in x {
let value = ChartDataEntry(x: Double(index), y: Double(y[index]))
values.append(value)
}

let set1 = ChartDataSet(entries: values, label: "Set1")
let data = ChartData(dataSet: set1)

// Some debug output
if data.entryCount > 0 {
let yValue = "\(values[data.entryCount-1].y)"
print("Y : \(yValue)")
for value in values {
print("\(value.y) ", terminator: "")
}
}
self.lineChart.data = data
}

}

Storyboard设置如下: enter image description here

enter image description here

最佳答案

尝试使用以下...

let x = [0,1,2,3,4,5,6,7,8,9]
let y = [0,1,2,3,4,5,6,7,8,9]
func setChartValues() {

var values = [ChartDataEntry]()
for index in x {
let value = ChartDataEntry(x: Double(index), y: Double(y[index]))
values.append(value)
}

let set1 = LineChartDataSet(entries: values, label: "Set1")
set1.color = UIColor.blue // red // green whatever you want.
let data = LineChartData()
data.addDataSet(data:set1)

// Some debug output
if data.entryCount > 0 {
let yValue = "\(values[data.entryCount-1].y)"
print("Y : \(yValue)")
for value in values {
print("\(value.y) ", terminator: "")
}
}
self.lineChart.data = data
}

关于swift - iosChart 不在 UITableViewCell 中显示 LineChartView 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57711264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com