所以我有一個tableView與每個單元格中的一組按鈕。這些是用戶從中進行投票的選項。iOS,加載在tableView單元格中的圖表也會加載到另一個單元格中?
當用戶按下一個選項中,圖表的負載示出投票結果。
然而,當我向下滾動,圖表莫名其妙地出現在另一個的tableView細胞。該單元格中的按鈕都沒有被按下過。此外,圖表會使用第一個數據加載第二個單元格。我一直堅持這幾個小時,不知道這是爲什麼。
編輯:下面是的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)
}
似乎你有一個錯誤。祝你好運 –
你是否異步加載圖表?你能告訴我們單元代碼嗎?我有一個想法是你的問題,但想先確認一下。 –
剛剛編輯並添加了代碼。 – bigreddawg