繁体   English   中英

滚动后表格视图单元格标签更改并在其中获得错误的值该如何解决?

[英]Table view cell label get changed after scrolling and get wrong value in it how to solve it?

每次我向上或向下滚动值时,在1处用错误的值更改它都会显示正确的值,但在滚动后每次都会更改。

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    //creating a cell using the custom class
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ShowPercentageAttandanceTableViewCell




    cell.rollNo.text = String(describing: self.mainArrayRoll[indexPath.row])


        var location = Int()

        for item in self.rollPercentage {
            if item.rollCall == indexPath.row {
                location = item.absentCount  
                cell.percentage.text=String(location)
                }

            else if cell.percentage.text==("Label")
            {

                cell.percentage.text=String("0")
            }

    }


    return cell
    }

这是代码。

[![图像2中的标签的文本为= 2,灰色标签的文本为0,但在滚动后它会自动更改,如图2所示。[![图像2的标签在滚动后发生变化,但显示错误] [2 ]] [2]

在此处输入图片说明

当找不到要查找的值并且单元格已重用时,您无法将cell.percentage.text设置为"0"

尝试这个:

if let item = self.rollPercentage.first(where: { $0.rollCall == indexPath.row }) {
    location = item.absentCount  
    cell.percentage.text = String(location)
} else {
    cell.percentage.text = "0"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM