gpt4 book ai didi

ios - UITableView cellForRowAt if 语句错误工作

转载 作者:行者123 更新时间:2023-11-29 11:47:44 25 4
gpt4 key购买 nike

根据下面的代码,我第一次运行应用程序。前 3 行工作正常。但是当我上下滚动时。一些线路也会改变。当我做更多的上下它更改为其他行

我该如何解决这个问题

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = KategoriEkleCell()
var KategoriId :String = ""


if tableView == tableView {
cell.selectionStyle = .none
cell = self.tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath as IndexPath) as! KategoriEkleCell
cell.itemLabel.text = (AltKategoriKayit[indexPath.section][indexPath.row] as? [String:String])?["kategoriadi"]
cell.itemLabel.font = UIFont(name:"OpenSans-Light", size: 14.0)

KategoriId = ((AltKategoriKayit[indexPath.section][indexPath.row] as? [String:String])?["kategoriid"])!
if (KategoriId) == (MenuKategoriKayit[indexPath.row]["kategoriid"]!) {

cell.EkleButton.layer.cornerRadius = 2
cell.EkleButton.layer.borderWidth = 1
cell.EkleButton.layer.borderColor = UIColor(red: 255/255, green: 56/255, blue: 107/255, alpha: 1.0).cgColor
cell.EkleButton.backgroundColor = UIColor(red: 255/255, green: 56/255, blue: 107/255, alpha: 1.0)
cell.EkleButton.setTitleColor(UIColor.white, for: .normal)
cell.EkleButton.clipsToBounds = true

}else {

cell.EkleButton.layer.cornerRadius = 2
cell.EkleButton.layer.borderWidth = 1
cell.EkleButton.layer.borderColor = UIColor(red: 255/255, green: 56/255, blue: 107/255, alpha: 1.0).cgColor
cell.EkleButton.clipsToBounds = true

}
}
return cell
}

合适的员工 Right employee

有问题的员工。当我上下滚动时。 Faulty employee. when I scroll up and down.

最佳答案

如评论中所述,单元格被重复使用。

您必须确保任何 UI 元素都设置为 cellForRow 中定义的状态。我将在 ifelse 分支中执行的重复行移出了 if - else 范围以提高可读性。

顺便说一句,if 行中的括号也不需要

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let specialRed = UIColor(red: 1.0, green: 56/255, blue: 107/255, alpha: 1.0)
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath) as! KategoriEkleCell
cell.selectionStyle = .none
cell.itemLabel.text = (AltKategoriKayit[indexPath.section][indexPath.row] as? [String:String])?["kategoriadi"]
cell.itemLabel.font = UIFont(name:"OpenSans-Light", size: 14.0)

let KategoriId = ((AltKategoriKayit[indexPath.section][indexPath.row] as? [String:String])?["kategoriid"])!
cell.EkleButton.layer.cornerRadius = 2
cell.EkleButton.layer.borderWidth = 1
cell.EkleButton.clipsToBounds = true
cell.EkleButton.layer.borderColor = specialRed.cgColor

if KategoriId == MenuKategoriKayit[indexPath.row]["kategoriid"]! {

cell.EkleButton.backgroundColor = specialRed
cell.EkleButton.setTitleColor(UIColor.white, for: .normal)
} else {
cell.EkleButton.backgroundColor = UIColor.white
cell.EkleButton.setTitleColor(specialRed, for: .normal)
}
return cell
}

请注意,根据命名约定,变量名应该以小写字母开头。

关于ios - UITableView cellForRowAt if 语句错误工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42759959/

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