gpt4 book ai didi

ios - 不应该在其他单元格上重复的表格单元格背景颜色

转载 作者:行者123 更新时间:2023-11-28 11:22:58 25 4
gpt4 key购买 nike

以下代码片段在应用于 UITableView 时应该只会使第一个单元格变为橙色,但也会使第 15 个单元格变为橙色。知道如何让一个单元格变成橙色吗?或者这是一个错误?我正在使用 ios8。

class ViewController: UITableViewController {

var links: [Int] = []

override func viewDidLoad() {
for index in 1...100 {
links.append(index)
}

}

@IBOutlet var aTableView: UITableView!

override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return links.count
}

override func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!) {

if indexPath.row == 0 {
cell.backgroundColor = UIColor.orangeColor()
}
}

最佳答案

这不是错误,这是一个功能! (但确实如此)。

你看,这些单元格被重用。如果您希望单元格 0 为橙色,则必须确保其他单元格为它们的常规颜色。

像这样:

if indexPath.row == 0 {
cell.backgroundColor = UIColor.orangeColor()
} else {
cell.backgroundColor = UIColor.whiteColor()
}

关于ios - 不应该在其他单元格上重复的表格单元格背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25330439/

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