every time I scroll up or down value get changed with wrong value at 1st it shows correct value but after scrolling it get changed every time.
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
}
Here is the code.
[![Here in image 2 label is having text =2 and gray color label is having text 0 but it automatically get change after scroll as shown in image 2[![image 2 label changes after scroll but it is showing wrong][2]][2]
You are failing to set the cell.percentage.text
to "0"
when you didn't find the value you were looking for and you had a reused cell.
Try this:
if let item = self.rollPercentage.first(where: { $0.rollCall == indexPath.row }) {
location = item.absentCount
cell.percentage.text = String(location)
} else {
cell.percentage.text = "0"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.