我在自定义的tableview单元格中使用来自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)
}
这是tableview单元格中的图表功能:
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)
}
答案 0 :(得分:0)
首先,在设置聊天视图后添加analyticsView.data = lineChartData
。它需要阅读一些属性来做一些数学运算;
第二,何时何地将图表视图添加到单元格的内容视图中?
另外,在didSelectRow中调用tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
时似乎有点奇怪,我假设您应该将图表视图添加到单元格的内容视图中,而不是重新加载数据?因为它会触发另一个cellForItemAtIndexPath
并覆盖你所做的事情?
基本上当你addSubView
时,它将开始绘制图表。您可以在折线图视图的addSubView
和drawRect
处添加断点,并查看在您点击单元格时它是否会中断。
您也可以按chartView.setneedsDisplay
更新
如果要重置动画,请再次调用animate API,也可以调用另一个setNeedsDisplay