滚动时如何防止 UILabel.textColor 在 dequeueReusableCell 中更改? [英] How to prevent UILabel.textColor to change in a dequeueReusableCell when scrolling?

查看:57
本文介绍了滚动时如何防止 UILabel.textColor 在 dequeueReusableCell 中更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为 PlayViewController 的 ViewController 中有这段代码:

I have this code in my ViewController named 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
        }
    }
}

所以基本上,该应用程序执行以下操作:

So basically, the app does the following stuff:

  • 它以黑色提示列表的第一个单词.
  • 向右滑动会将单词的颜色更改为绿色,向左滑动将更改为红色.滑动还会以黑色提示下一个单词.
  • numberOfRevealedLabels 统计当前显示的单词数,indexGA 有助于跟踪单词变为绿色的位置,indexWA 也是如此对于红色的.
  • It prompts the first word of the list in black.
  • Swiping right changes the color of the word to green, left to red. Swipes also prompts the next word in black.
  • numberOfRevealedLabels counts the number of words currently displayed, indexGA helps to track the position of the word to turn in green, same for indexWA for the red ones.

当我向下滚动阅读下一个单词时(我从第十二个单词开始滚动),它以与前一个单词相同的颜色提示(向右滑动时为绿色,向左滑动时为红色).此外,列表中的第一个单词会随机改变颜色(黑色、绿色或红色).

When I scroll down to read the next word (I start scrolling at the twelfth word), it's prompted in the same color of the previous word (green if I swiped right or red if left). Also, the first words of the list change their color randomly (either black, green or red).

我读过类似这些 ones 我知道这是因为 dequeueReusableCell.

I've read threads like these ones and I know it's because of dequeueReusableCell.

我的代码没有 else 条件.当添加一个将 .textColor 设置为黑色时,会将所有单词都变成黑色,但最后两个变成黑色.

My code has no else condition. When adding one to set the .textColor to black turns all the words but the last two swiped to black.

要修复 else 语句,我可以编写如下代码:

To fix the else statement, I can code something like this:

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).

Unfortunately, it didn't solve my problem, labels in cells keep changing colors in a weird fashion (plus, it adds a few more ugly LOC to a not so logic loop).

我尝试的最后一件事:在 PlayTableViewCell.swift 中设置 wordLabel.textColor = UIColor.black 但它没有修复任何东西.

Last thing I've tried: to set wordLabel.textColor = UIColor.blackin PlayTableViewCell.swiftbut it didn't fixed anything.

我没有想法/逻辑,任何帮助将不胜感激!

I'm out of ideas/logic, any help would be very much appreciated!

推荐答案

你应该在调用 cell.wordLabel.text = words 后立即设置 cell.wordLabel.textColor = UIColor.black[indexPath.row].

You should set cell.wordLabel.textColor = UIColor.black right after you call cell.wordLabel.text = words[indexPath.row].

所以它应该是这样的:

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 语句 之外添加它将确保颜色始终为黑色,除非满足条件.

Adding an else doesn't solve the problem because it's placed in a nested if statement. A cell with the color can still be reused since you aren't setting a default color before any if statements. Adding it outside of all if statements will ensure the color is always black unless a condition is met.

这篇关于滚动时如何防止 UILabel.textColor 在 dequeueReusableCell 中更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆