2015-11-09 31 views
0

我正在使用自定義tableview單元格中的iOS-Charts中的線形圖表進行觸摸擴展。我沒有加載cellForRow中的圖表:因爲這加載了所有單元格中的所有圖表並且殺死了滾動性能,所以我在didSelectRow中加載了它:問題是,出於某種原因,圖表不會在用戶第一次觸及單元格時加載,我不知道爲什麼。另外,當展開的單元格(當前正在顯示圖表)被摺疊時,似乎圖表動畫開始。如何重置tableview單元格中的iOS-Charts動畫

這是我的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在實現代碼如下細胞:

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

第一,把analyticsView.data = lineChartData你設置你的聊天視圖後。它需要閱讀一些屬性來做一些數學運算;

秒,何時何地將圖表視圖添加到單元格的內容視圖中?

另外,當您在didSelectRow中調用tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)時,我認爲您應該將圖表視圖添加到單元格的內容視圖中,而不是重新載入數據?因爲它會觸發另一個cellForItemAtIndexPath並覆蓋你所做的事情?

基本上當你做addSubView時,它會開始繪製圖表。您可以在折線圖視圖的addSubViewdrawRect上添加一個折點,並查看它在擊中單元格時是否會中斷。

您也可以手動觸發通過chartView.setneedsDisplay

更新重繪:

如果你想重置動畫,再次調用API動畫也許再setNeedsDisplay

+0

我改變了'analyticsView.data = lineChartData 「在建立圖表視圖後最終結束;它仍然運行相同。當我在didSelectRow中調用'cell.setChart(dataForMonths,values:dataForRatings)'時添加圖表視圖,並顯示在我的單元格中的UIView中。 – tahoecoop

+0

然後你應該在drawRect被調用時開始調試 – Wingzero

相關問題