gpt4 book ai didi

ios - UITableView 自定义单元格数据在快速滚动时不匹配

转载 作者:行者123 更新时间:2023-11-30 12:18:51 26 4
gpt4 key购买 nike

    class CalenderCell: UITableViewCell {

@IBOutlet var lblDay: UILabel!
@IBOutlet var lblRest: UILabel!
@IBOutlet var imgCompleted: UIImageView!

override func awakeFromNib()
{
super.awakeFromNib()
}

func updateCell(exerciseobject : Exercise)
{
let resttext = NSLocalizedString("restday", comment: "")
let day = NSLocalizedString("day", comment: "")
let dayexercise = "\(day) \(exerciseobject.exerciseDayID)"

if !exerciseobject.exerciseRestDay
{
lblDay.text = dayexercise
if exerciseobject.exerciseDayStatus
{
imgCompleted.isHidden = false
}
else
{
imgCompleted.isHidden = true
}
lblRest.isHidden = true

}
else
{
lblDay.isHidden = true
imgCompleted.isHidden = true
lblRest.text = resttext
viewWithTag(100)?.backgroundColor = UIColor(red:1.00, green:0.21, blue:0.28, alpha:1.0)
}
}
}

上面是我的自定义单元格类,下面是我的表格 View 类。
当我尝试构建应用程序时,一切都工作正常,但每当我快速滚动时,数据就会不匹配或相同的数据出现在更多单元格中。

我尝试过:

prepareforreuse() 
{
//clearing label and images here
}

在我的自定义单元格类中,但仍然出现快速 ScrollView 数据不匹配

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
if let cell = calenderTable.dequeueReusableCell(withIdentifier: "CalenderCell", for: indexPath) as? CalenderCell
{

cell.updateCell(exerciseobject: days[indexPath.row])
return cell
}

else
{
return UITableViewCell()
}
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return days.count
}

最佳答案

使用 prepareForReuse 并将控件重置为默认值,或者确保更新 updateCell 方法中每个路径的所有状态。

例如

func updateCell(exerciseobject : Exercise)
{
let resttext = NSLocalizedString("restday", comment: "")
let day = NSLocalizedString("day", comment: "")
let dayexercise = "\(day) \(exerciseobject.exerciseDayID)"

if !exerciseobject.exerciseRestDay
{
lblDay.text = dayexercise
lblDay.isHidden = false
if exerciseobject.exerciseDayStatus
{
imgCompleted.isHidden = false
}
else
{
imgCompleted.isHidden = true
}
lblRest.isHidden = true

viewWithTag(100)?.backgroundColor = nil
}
else
{
lblDay.isHidden = true
imgCompleted.isHidden = true
lblRest.text = resttext
lblRest.isHidden = false
viewWithTag(100)?.backgroundColor = UIColor(red:1.00, green:0.21, blue:0.28, alpha:1.0)
}
}

关于ios - UITableView 自定义单元格数据在快速滚动时不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45050602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com