gpt4 book ai didi

ios - 如何修复 swift 4 中的 UITableView 单元格重复

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

当我创建一个新单元格时,它会自动标记或者当我标记第一个时,我正在创建多个单元格,会有重复标记

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

let itemCellIdentifier = "itemCell"

guard let itemCell = itemCardTableView.dequeueReusableCell(withIdentifier: itemCellIdentifier, for: indexPath) as? ItemCardTableViewCell else {
return UITableViewCell()
}

let itemCard = dataManager.items[indexPath.row]
itemCell.itemTitle.text = itemCard.title

if itemCard.isFinish {
itemCell.itemCellView.backgroundColor = UIColor(red: 0, green: 123, blue: 0)
}
return itemCell
}

添加单元格方法

let confirm = UIAlertAction(title: "確認", style: .default) { (action: UIAlertAction) in

guard let title = addAlert.textFields?.first?.text else { return }

let newItem = ItemCard(title: title, isFinish: false)
self.dataManager.items.append(newItem)

let indexPath = IndexPath(row: self.itemCardTableView.numberOfRows(inSection: 0), section: 0)
self.itemCardTableView.insertRows(at: [indexPath], with: .left)

当我创建新数据时 isFinish = false

如何修复数据重复?

最佳答案

您应该提供其他情况来设置默认背景颜色。

if itemCard.isFinish {
itemCell.itemCellView.backgroundColor = UIColor(red: 0, green: 123, blue: 0)
} else {
itemCell.itemCellView.backgroundColor = UIColor.white
}

关于ios - 如何修复 swift 4 中的 UITableView 单元格重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50896895/

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