滚动表视图时,TableViewCell中的ProgressView进度会更改

时间:2018-08-03 08:04:13

标签: ios swift uitableview swift3 uiprogressview

在我的应用中,我必须在一个tableViewCell中显示多个进度视图,进度是按实际进度达到目标的进度,并且根据image1首次加载单元时可以正确显示进度

image1

但是在滚动表格后,该单元格就像image2一样可见。

image2

滚动表视图后,将更改进度视图的进度。我已经用cellForRowAt方法打印了进度值,它们是正确的,但是进度视图中的进度已被更改。我正在使用cellForRowAt方法设置progressview进度。

有人可以帮助我解决此问题吗?

@Bhavin Kansagara,这是我在cellForRowAt方法中的代码。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cellIdentifier = "myIdentifier"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyTableViewCell
        if cell == nil {
            tableView.register(UINib.init(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
            cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyTableViewCell
        }

        let myDict = self.myArray[indexPath.row]
        let goalDict = myDict["goal"]!
        let actualDict = myDict["actual"]!

        for label in cell!.actualValueLabels {
            switch label.tag {
            case 0:
                label.text = String(actualDict["value1"]!)
            case 1:
                label.text = String(actualDict["value2"]!)
            case 2:
                label.text = String(actualDict["value3"]!)
            case 3:
                label.text = String(actualDict["value4"]!)
            case 4:
                label.text = String(actualDict["value5"]!)
            case 5:
                label.text = String(actualDict["total"]!)
            default:
                break
            }
        }

        for label in cell!.goalValueLabels {
            switch label.tag {
            case 0:
                label.text = String(goalDict["value1"]!)
            case 1:
                label.text = String(goalDict["value2"]!)
            case 2:
                label.text = String(goalDict["value3"]!)
            case 3:
                label.text = String(goalDict["value4"]!)
            case 4:
                label.text = String(goalDict["value5"]!)
            case 5:
                label.text = String(goalDict["total"]!)
            default:
                break
            }
        }

        let greenColor = UIColor(red: 146/255, green: 209/255, blue: 78/255, alpha: 1)
        let yellowColor = UIColor(red: 1, green: 122/255, blue: 0, alpha: 1)

        let value1Goal = goalDict["value1"]!
        let value1Actual = actualDict["value1"]!
        cell?.value1ProgressView.progressTintColor = value1Actual >= value1Goal ? greenColor : yellowColor
        if value1Goal == 0 {
            DispatchQueue.main.async {
                cell?.value1ProgressView.setProgress(Float(value1Actual), animated: true)
            }
        } else {
            DispatchQueue.main.async {
                cell?.value1ProgressView.setProgress(Float(value1Actual)/Float(value1Goal), animated: true)
            }
        }
        let value2Goal = goalDict["value2"]!
        let value2Actual = actualDict["value2"]!
        cell?.value2ProgressView.progressTintColor = value2Actual >= value2Goal ? greenColor : yellowColor
        if value2Goal == 0 {
            DispatchQueue.main.async {
                cell?.value2ProgressView.setProgress(Float(value2Actual), animated: true)
            }
        } else {
            DispatchQueue.main.async {
                cell?.value2ProgressView.setProgress(Float(value2Actual)/Float(value2Goal), animated: true)
            }
        }
        let value3Goal = goalDict["value3"]!
        let value3Actual = actualDict["value3"]!
        cell?.value3ProgressView.progressTintColor = value3Actual >= value3Goal ? greenColor : yellowColor
        if value3Goal == 0 {
            DispatchQueue.main.async {
                cell?.value3ProgressView.setProgress(Float(value3Actual), animated: true)
            }
        } else {
            DispatchQueue.main.async {
                cell?.value3ProgressView.setProgress(Float(value3Actual)/Float(value3Goal), animated: true)
            }
        }
        let value4Goal = goalDict["value4"]!
        let value4Actual = actualDict["value4"]!
        cell?.value4ProgressView.progressTintColor = value4Actual >= value4Goal ? greenColor : yellowColor
        if value4Goal == 0 {
            DispatchQueue.main.async {
                cell?.value4ProgressView.setProgress(Float(value4Actual), animated: true)
            }
        } else {
            DispatchQueue.main.async {
                cell?.value4ProgressView.setProgress(Float(value4Actual)/Float(value4Goal), animated: true)
            }
        }
        let value5Goal = goalDict["value5"]!
        let value5Actual = actualDict["value5"]!
        cell?.value5ProgressView.progressTintColor = value5Actual >= value5Goal ? greenColor : yellowColor
        if value5Goal == 0 {
            DispatchQueue.main.async {
                cell?.value5ProgressView.setProgress(Float(value5Actual), animated: true)
            }
        } else {
            DispatchQueue.main.async {
                cell?.value5ProgressView.setProgress(Float(value5Actual)/Float(value5Goal), animated: true)
            }
        }
        let totalGoal = goalDict["total"] as! Int
        let totalActual = actualDict["count"]!
        cell?.totalProgressView.progressTintColor = totalActual >= totalGoal ? greenColor : yellowColor
        if totalGoal == 0 {
            DispatchQueue.main.async {
                cell?.totalProgressView.setProgress(Float(totalActual), animated: true)
            }
        } else {
            DispatchQueue.main.async {
                cell?.totalProgressView.setProgress(Float(totalActual)/Float(totalGoal), animated: true)
            }
        }

        return cell!
    }

    else {
        //another cells are used in other sections
    }
}

此处myArray = [[String: [String: Int]]]

谢谢。

1 个答案:

答案 0 :(得分:0)

有一种方法可以处理这种情况,您应该尝试为单元设置不同的标识符,但这会影响您的性能。设置此方法后,您应该检查设备上滚动的性能。

let cellIdentifier = "myIdentifier_\(indexPath.section)_\(indexPath.row)"
    var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyTableViewCell
    if cell == nil {
        tableView.register(UINib.init(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
        cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? MyTableViewCell
}

这不是最好的方法,但可以帮助避免重复过程的问题。

尝试并分享结果。