gpt4 book ai didi

ios - 如何在表格 View 单元格中重置 iOS 图表动画

转载 作者:行者123 更新时间:2023-11-30 13:55:56 25 4
gpt4 key购买 nike

我在自定义表格 View 单元格中使用 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)
}

这是表格 View 单元格中的图表函数:

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)
}

最佳答案

首先,在设置聊天 View 后输入 analyticsView.data = lineChartData。它需要读取一些属性来进行一些数学运算;

第二,何时何地将图 TableView 添加到单元格的内容 View 中?

此外,当您在 didSelectRow 中调用 tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic) 时,这似乎有点奇怪,我认为您应该将图 TableView 添加到单元格的内容 View 中,而不是重新加载数据?因为它会触发另一个 cellForItemAtIndexPath 并覆盖您所做的事情?

基本上,当您执行addSubView时,它将开始绘制图表。可以在折线图 View 的addSubViewdrawRect处添加断点,看看点击单元格时能否断点。

您还可以通过chartView.setneedsDisplay手动触发重绘

更新:

如果您想重置动画,请再次调用 animate API,也许还可以调用另一个 setNeedsDisplay

关于ios - 如何在表格 View 单元格中重置 iOS 图表动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33618719/

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