2017-08-08 15 views
-2

私は各セルにボタンのセットを持つtableViewを持っています。これらはユーザーが投票するためのオプションです。iOS、tableViewのセルにロードされているチャートも別のセルに読み込まれますか?

TableView with options

ユーザがオプション、投票の結果を示すグラフ負荷を押します。

Poll results after button pressed

私は下にスクロールするときただし、グラフが別のtableView細胞に不可解表示されます。そのセルのどのボタンも押されていませんでした。さらに、グラフは、最初のセルのデータで2番目のセルに読み込まれます。私は何時間もこれに固執していて、それがなぜなのか分かりません。

enter image description here

EDIT:ここにtableView

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

let cell = tableView.dequeueReusableCell(withIdentifier: "pollCell") as! PollCell 

let titled = titles[indexPath.row] 
    cell.configure(polltitled: titled, num: indexPath.row) 

return cell 
} 

ためのコードであり、ここで投票電池用メソッドです:

var ref = FIRDatabase.database().reference() 

var options = [String]() 
var counts = [Int]() 

self.loadedChart = false 

@IBOutlet weak var chart: HorizontalBarChartView! 

func configure(polltitled: String, num: Int) { 

    self.pollTitle.text = polltitled 
    self.row = num 
    self.runRemove() { (success) -> Void in 
     if success { 
      if (loadedChart == false) { 
       self.loadChart() 
      } 
     } 
     else { 

     } 
     } 
} 

func runRemove(completion:(_ success: Bool) -> Void) { 
     self.pollTitle.tag = 999 
     self.chart.tag = 998 

     for object in self.contentView.subviews { 
      if ((object.tag != 999) && (object.tag != 998)) { 
       object.removeFromSuperview() 

       } 

    } 

     completion(true) 
} 

func loadChart(){ 
    ref.child("poll").child(StaticVariables.currentEventQR).child(self.pollTitle.text!).observe(.value) { (snapshot: FIRDataSnapshot!) in 


     self.counts.removeAll() 
     self.options.removeAll() 
     for item in snapshot.children { 

      let childSnapshot = snapshot.childSnapshot(forPath: (item as AnyObject).key) 

      let option = childSnapshot.key 

      self.options.append(option) 

     } 
     self.loadCounts() 
     self.createButtons() 
    } 

} 

func loadCounts() { 

    for i in self.options { 
     self.ref.child("poll").child(StaticVariables.currentEventQR).child(self.pollTitle.text!).child(i).observe(.value) { (snapshot: FIRDataSnapshot!) in 

      for item in snapshot.children { 

        let childSnapshot = snapshot.childSnapshot(forPath: (item as AnyObject).key) 

        let countsValue = childSnapshot.value as? NSDictionary 
        let count = countsValue?["Counter"] as! Int 
        self.counts.append(count) 


      } 

     } 


    } 




} 

func createButtons() { 
    var i = 1 
    var height = 0 
    if ((self.pollTitle.text?.characters.count)! > 37){ 
     height = 22 
    } 
    for option in self.options{ 
     let btn: UIButton = UIButton(frame: CGRect(x: Int(self.contentView.frame.size.width - 320)/2, y: 59+60*(i-1)+height, width: 320, height: 33)) 
     if ((i)%2 == 1){ 
      btn.backgroundColor = UIColor.init(red: 253/255, green: 185/255, blue: 39/255, alpha: 1.0) 
     } else { 
      btn.backgroundColor = UIColor.init(red: 0/255, green: 107/255, blue: 182/255, alpha: 1.0) 
     } 

     btn.addTarget(self, action: #selector(PollCell.optionOne(_:)), for: .touchUpInside) 
     btn.tag = i 
     self.contentView.addSubview(btn) 
     btn.setTitle(option, for: .normal) 
     i += 1 

    } 
} 

@IBAction func optionOne(_ sender: UIButton) { 
    self.loadedChart = true 
    delegate.refresh(self.row) 
    self.options.reverse() 
    self.counts.reverse() 
    self.setChart(dataPoints: self.options, values: self.counts) 
    ref.child("poll").child(StaticVariables.currentEventQR).child(pollTitle.text!).child((sender.titleLabel?.text!)!).observeSingleEvent(of: .value, with: { (snapshot) in 


     let item = snapshot.children.allObjects[0] 
     let childSnapshot = snapshot.childSnapshot(forPath: (item as AnyObject).key) 

     let responseID = childSnapshot.key 
     let reponseValue = childSnapshot.value as? NSDictionary 
     var response = reponseValue?["Counter"] as! Int 
     response += 1 
     self.ref.child("poll").child(StaticVariables.currentEventQR).child(self.pollTitle.text!).child((sender.titleLabel?.text!)!).child("count").setValue(["Counter": response]) 

    }) 
    self.contentView.bringSubview(toFront: self.chart) 

} 


func setChart(dataPoints: [String], values: [Int]) { 

    self.chartHeight.constant = CGFloat((RowHeightCounter.sharedInstance.counters[row])*62+39) 

    chart.chartDescription?.text = "" 
    chart.noDataText = "" 
    var dataEntries: [BarChartDataEntry] = [] 

    for i in 0..<dataPoints.count { 
     let dataEntry = BarChartDataEntry(x: Double(i), y: Double(counts[i])) 
     dataEntries.append(dataEntry) 
    } 

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Votes") 
    let chartData = BarChartData(dataSet: chartDataSet) 
    chart.data = chartData 
    chart.xAxis.valueFormatter = IndexAxisValueFormatter(values: options) 

    chart.xAxis.granularity = 1 
    self.chart.alpha = 1.0 
    chart.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce) 

} 
+0

あなたにバグがあるようです。幸運 –

+0

グラフを非同期で読み込みますか?そして、私たちに細胞コードを教えてください、私はあなたの問題が何であるかを知っていますが、それを最初に確認したいと思います。 –

+0

コードを編集して追加しました。 – bigreddawg

答えて

0

問題はそのUITableView再利用では、常に新しいものを作り出すのではなく、テーブルに十分な行がある場合は、スクロールしたときにチャートが何回も表示されます。

セルにチャートを追加すると、セルが削除されるまでそこに表示されるので、そのセルが再び使用されるたびにチャートが表示されます。

UITableViewCellには、再利用しようとするときにセル化されるconvientメソッドがあります。prepareForReuse()このメソッドを実装し、再度表示する前にリセットする必要があるセル内のすべてをリセットします。

これは、セルとデータソースの間のインターフェイスを再考する必要がある可能性があることを意味します。たぶん、ユーザーがそれに答えたかどうかにかかわらず、質問と結果のデータを保持するモデルオブジェクトを作成します。データソースは一連の質問を保持し、正しい質問を各セルに渡すことができます。セルは、質問に基づいてセルを構成することができます。

問題から結果に変更するときは、さまざまなビューを削除しません。 1つのコンテナビューにすべての質問を入れ、別のコンテナビューにすべてのチャートを入れる。次に、必要に応じて2つのコンテナにisHiddenを設定します。これにより、スクロールするフレームレートが高く保たれます。