0

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

1 回答 1

0

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

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

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

基本上当你这样做时addSubView,它会开始绘制图表。addSubView您可以在折线图视图的和处添加断点drawRect,并查看当您点击单元格时它是否会中断。

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

更新:

如果要重置动画,请再次调用 animate API,可能还有另一个 setNeedsDisplay

于 2015-11-10T05:33:30.837 回答