【问题标题】:ProgressView progress in TableViewCell is altered when table view is scrolled滚动表格视图时,TableViewCell 中的 ProgressView 进度会更改
【发布时间】:2019-01-11 01:43:39
【问题描述】:

在我的应用程序中,我必须在单个 tableViewCell 中显示多个进度视图,进度按照实际到目标,并且在第一次加载单元格时按照 image1 正确显示

但滚动表格后,单元格就像 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]]]

提前致谢。

【问题讨论】:

  • 添加您的代码以轻松指出问题。
  • 您需要在 ModelClass 中存储进度值,一旦进度值更新,您还需要在模型中更新该值并始终从模型中加载值。
  • @BhavinKansagara 根据我添加的代码,您有什么解决方案吗?谢谢
  • 看你的代码,一切正常。但由于 tableView 重用单元格,一个转移到另一个的进度。您能否检查 MyTableViewCell 中 Progressview 的 Outlet,如果之前很弱,则将其更改为强。并重新运行
  • 我将网点改成 strong 并重新运行,但问题没有解决

标签: ios swift uitableview swift3 uiprogressview


【解决方案1】:

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

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
}

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

尝试并分享结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多