2017-08-08 89 views
-2

所以我有一个tableView与每个单元格中的一组按钮。这些是用户从中进行投票的选项。iOS,加载在tableView单元格中的图表也会加载到另一个单元格中?

TableView with options

当用户按下一个选项中,图表的负载示出投票结果。

Poll results after button pressed

然而,当我向下滚动,图表莫名其妙地出现在另一个的tableView细胞。该单元格中的按钮都没有被按下过。此外,图表会使用第一个数据加载第二个单元格。我一直坚持这几个小时,不知道这是为什么。

enter image description here

编辑:下面是的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有一个简便的方法,在即将被重用的时候得到celled,prepareForReuse()。实施此方法并重置需要重置的单元格中的任何内容,然后再次出现。

这确实意味着您可能需要重新考虑单元和数据源之间的接口。也许创建一个模型对象来保存问题和结果数据以及用户是否回答。数据源可以包含一系列问题并将正确的问题传递给每个单元格。然后单元可以根据问题进行自我配置

在附注中,从问题转换为结果时,我不会删除各种视图。我将包含所有问题的东西在一个容器视图和所有图表的东西在另一个。然后根据需要在两个容器上设置isHidden。这将保持您的滚动帧率更高。

相关问题