我在自定义表格视图单元格中使用来自 iOS-Charts 的折线图,该单元格在触摸时扩展。我没有在 cellForRow: 中加载图表,因为这会加载所有单元格中的所有图表并破坏滚动性能,所以我将它加载到 didSelectRow: 中。问题是由于某种原因,图表在用户第一次触摸单元格时没有加载,我不知道为什么。此外,图表动画似乎是在展开的单元格(当前显示图表)折叠时开始的。
这是我的 didSelectRow: 下面是我的自定义单元格中的图表函数。我一直在努力解决这个问题,任何见解将不胜感激!
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell:CandDetailTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as! CandDetailTableViewCell
// CALLS METHOD TO BUILD CHART WITH DATA I SAVED IN THESE ARRAYS
cell.setChart(dataForMonths, values: dataForRatings)
let previousIndexPath = selectedIndexPath
if indexPath == selectedIndexPath {
// if user taps cell that was already selected so it collapses
selectedIndexPath = nil
} else {
// when user selects new cell
selectedIndexPath = indexPath
}
var indexPaths:Array <NSIndexPath> = []
if let previous = previousIndexPath {
indexPaths += [previous]
}
if let current = selectedIndexPath {
indexPaths += [current]
}
if indexPaths.count > 0 {
tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
}
tableView.scrollRectToVisible(tableView.rectForRowAtIndexPath(indexPath), animated: true)
}
这是表格视图单元格中的图表函数:
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Rating")
let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
analyticsView.data = lineChartData
analyticsView.xAxis.labelPosition = .Bottom
analyticsView.backgroundColor = UIColor.whiteColor()
analyticsView.userInteractionEnabled = false
analyticsView.drawGridBackgroundEnabled = false
analyticsView.drawBordersEnabled = false
analyticsView.leftAxis.enabled = false
analyticsView.rightAxis.enabled = false
analyticsView.xAxis.gridLineWidth = 0.0
analyticsView.xAxis.axisLineColor = UIColor.clearColor()
analyticsView.legend.enabled = false
// Removes the description text off the chart
analyticsView.descriptionText = ""
analyticsView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
}