我的 ViewController 中有这段代码,名为 PlayViewController
:
var words = [String]()
var numberOfRevealedLabels = 1
var indexGA = 0
var indexWA = 0
override func viewDidLoad() {
super.viewDidLoad()
playTableView.delegate = self
playTableView.dataSource = self
view.isUserInteractionEnabled = true
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = .right
view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeLeft.direction = .left
view.addGestureRecognizer(swipeLeft)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "PlayTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlayTableViewCell else {
fatalError("The dequeued cell is not an instance of PlayTableViewCell.")
}
cell.wordLabel.text = words[indexPath.row]
var count = 0
while (count < words.count)
{
if (indexPath.row == count) {
if (count == 0) {
cell.wordLabel.isHidden = false
}
if (indexGA - 1 == count) {
cell.wordLabel.textColor = UIColor.green
} else if (indexWA - 1 == count) {
cell.wordLabel.textColor = UIColor.red
}
}
count += 1
}
cell.wordLabel.isHidden = !(indexPath.row <= numberOfRevealedLabels - 1)
return cell
}
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.right:
indexGA = numberOfRevealedLabels
numberOfRevealedLabels += 1
playTableView.reloadData()
case UISwipeGestureRecognizer.Direction.left:
indexWA = numberOfRevealedLabels
numberOfRevealedLabels += 1
playTableView.reloadData()
default:
break
}
}
}
基本上,该应用程序执行以下操作:
- 它会以黑色提示列表的第一个单词。
- 向右滑动可将单词的颜色更改为绿色,向左滑动可更改为红色。滑动还会以黑色提示下一个单词。
numberOfRevealedLabels
统计当前显示的单词数量,indexGA
帮助跟踪单词变为绿色的位置,与indexWA
相同对于红色的。
当我向下滚动阅读下一个单词时(我从第十二个单词开始滚动),它会以与前一个单词相同的颜色进行提示(如果向右滑动则为绿色,如果向左滑动则为红色)。此外,列表中的第一个单词会随机更改其颜色(黑色、绿色或红色)。
我读过类似 these 的帖子ones我知道这是因为 dequeueReusableCell
。
我的代码没有 else
条件。当添加一个将 .textColor
设置为黑色时,除了最后两个之外的所有单词都会变成黑色。
要修复 else
语句,我可以编写如下代码:
else {
if (cell.wordLabel.textColor == UIColor.green) {
cell.wordLabel.textColor = UIColor.green
} else if (cell.wordLabel.textColor == UIColor.red) {
cell.wordLabel.textColor = UIColor.red
} else {
cell.wordLabel.textColor = UIColor.black
}
}
不幸的是,它并没有解决我的问题,单元格中的标签不断以一种奇怪的方式改变颜色(另外,它在一个不那么逻辑的循环中添加了一些更难看的LOC)。
我尝试的最后一件事:在 PlayTableViewCell.swift
中设置 wordLabel.textColor = UIColor.black
但它没有解决任何问题。
我没有想法/逻辑,任何帮助将非常感激!
最佳答案
您应该在调用 cell.wordLabel.text = Words[indexPath.row]
后立即设置 cell.wordLabel.textColor = UIColor.black
。
所以它应该看起来像这样:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "PlayTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlayTableViewCell else {
fatalError("The dequeued cell is not an instance of PlayTableViewCell.")
}
cell.wordLabel.text = words[indexPath.row]
cell.wordLabel.textColor = UIColor.black //HERE
var count = 0
while (count < words.count)
{
if (indexPath.row == count) {
if (count == 0) {
cell.wordLabel.isHidden = false
}
if (indexGA - 1 == count) {
cell.wordLabel.textColor = UIColor.green
} else if (indexWA - 1 == count) {
cell.wordLabel.textColor = UIColor.red
}
}
count += 1
}
cell.wordLabel.isHidden = !(indexPath.row <= numberOfRevealedLabels - 1)
return cell
}
添加 else
并不能解决问题,因为它被放置在嵌套的 if 语句
中。具有该颜色的单元格仍然可以重复使用,因为您没有在任何 if 语句
之前设置默认颜色。将其添加到所有 if 语句
之外将确保颜色始终为黑色,除非满足条件。
关于swift - 滚动时如何防止 UILabel.textColor 在 dequeueReusableCell 中发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54057392/