【问题标题】:How to reset iOS-Charts animation in tableview cell如何在 tableview 单元格中重置 iOS-Charts 动画
【发布时间】:2015-11-11 09:27:08
【问题描述】:

我在自定义表格视图单元格中使用来自 iOS 图表的折线图,该单元格在触摸时会扩展。我没有在 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)
       }

【问题讨论】:

    标签: ios swift ios-charts


    【解决方案1】:

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

    其次,您何时何地将图表视图添加到单元格的内容视图中?

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

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

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

    更新:

    如果要重置动画,请再次调用 animate API 并可能再次调用 setNeedsDisplay

    【讨论】:

    • 在设置图表视图后,我将analyticsView.data = lineChartData 更改为末尾;它仍然运行相同。当我在 didSelectRow: 中调用 cell.setChart(dataForMonths, values: dataForRatings) 并在单元格的 UIView 中显示时,我正在添加图表视图。
    • 那么你应该在调用drawRect时开始调试
    猜你喜欢
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 2011-10-27
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2017-07-01
    相关资源
    最近更新 更多