【问题标题】:dequeueReusableCell causes text to change colordequeueReusableCell 导致文本改变颜色
【发布时间】:2020-01-22 03:02:15
【问题描述】:

我有一个集合视图,我正在为单元格使用自定义类。每个单元格有两个文本视图,chapterTitleTextView 和 chapterBodyTextView。我在两个文本视图中都添加了占位符,如下所示:

class CustomWriterPageCell: UICollectionViewCell, UITextViewDelegate {

    // When the user taps on a text view
    func textViewDidBeginEditing(_ textView: UITextView) {

        if textView.textColor == .gray {

            textView.text = nil
            textView.textColor = .black
        }
    }

    // When the user taps out of a text view or taps on the done button in the toolbar
    func textViewDidEndEditing(_ textView: UITextView) {

        // If the chapter title text view is empty
        if chapterTitleTextView.text.isEmpty {

            chapterTitleTextView.text = "Chapter Title"
            chapterTitleTextView.textColor = .gray

        }
        // If the chapter body text view is empty
        if chapterBodyTextView.text.isEmpty {

            chapterBodyTextView.text = "Chapter Body"
            chapterBodyTextView.textColor = .gray

        }
    }
}

它的工作原理是,文本颜色最初是灰色的,并且有一些文本,当用户点击文本视图时,颜色变为黑色,文本视图中的文本被删除。

现在使用 dequeueReusableCell 存在这个问题,它重用了单元格,这导致了问题 1:无论我在第一个单元格的文本视图中键入的内容出现在第 4 个单元格上,为了解决这个问题,我必须创建2 个全局列表来保存我输入的任何内容,这会显示在单元格的文本视图中,代码如下:

全局列表:

var chapterTitleText = Array(repeating: "", count: 4) // To store the chapter title text
var chapterBodyText = Array(repeating: "", count: 4) // To store the chapter body text

下面的代码 sn-p 在之前的 textViewDidEndEditing 中

// Append the chapter body text to the chapterBodyText array
let titleText = chapterTitleTextView.text
let titleRow = textView.tag //This the indexPath.row
chapterTitleText[titleRow] = titleText!

// Append the chapter title text to the chapterTitleText array
let bodyText = chapterBodyTextView.text
let bodyRow = textView.tag
chapterBodyText[bodyRow] = bodyText!

在 cellForItemAt 中:

cell.chapterBodyTextView.tag = indexPath.row
cell.chapterTitleTextView.tag = indexPath.row

cell.chapterTitleTextView.text = chapterTitleText[indexPath.row]
cell.chapterBodyTextView.text = chapterBodyText[indexPath.row]

这消除了第 1 个问题(文本视图中的文本重复)。但是后来我遇到了一个新问题,还记得我说的占位符文本吗?当我在第一个单元格的一个文本视图中键入内容时,第四个单元格中的文本视图的文本颜色会发生变化。我将添加一个重现问题的 GIF 以帮助读者理解:

【问题讨论】:

    标签: ios swift textview uicollectionview


    【解决方案1】:

    根据您的一系列问题,我建议您每次与UICollectionViewUITableView 打交道时都这样做。

    在您的单元格类中定义一个方法,并获取它需要显示的任何数据作为参数:

    func configure(text : String?) { //text is optional here which means it can be nil.
    //However, from my previous answer you can replace it with an empty string condition.
    
        if let txt = text { //or if text != "" 
            self.chapterTitleTextView.text = txt
            self.chapterTitleTextView.text.textColor = .black
        }
        else {// As others mentioned make sure to always handle else because cells are reusable.
            self.chapterTitleTextView.text = "Chapter Title"
            self.chapterTitleTextView.text.textColor = .gray
        }
    
    } 
    

    可重用单元背后的直觉是,由于它们是可重用的,因此您应该完全重新配置它们,而不是期望配置被保存或附加到单元。

    现在,在cellForItemAt

     let cell = ...
     cell.configure(text : chapterTitleText[indexPath.row])
    

    请记住,通过这种方式,您不需要定义全局数组。正如我之前告诉你的,这个数组只需要在你的控制器中定义,你的单元不需要知道它。您的单元只需要知道通过configure 函数传递的该数组的一个索引。 虽然这个全局数组可以工作,但我说的是编码的适当性。

    用这种方法评论你的问题(如果有的话),我会尽量耐心回答,但不要指望(复制粘贴)可代码。

    【讨论】:

      【解决方案2】:

      这是因为UICollectionViewUITableView 的重用机制。由于您仅以一种方式更新颜色,因此集合“记住”以前的颜色,并且在新单元格中重新创建其以前的状态。这里有两个基本解决方案。

      首先是为这两种情况更新单元格状态,例如:

      if myCondition == true {
        color = UIColor.gray
      } else {
        color = UIColor.black
      }
      

      第二是利用UICollectionViewCellprepareForReuse方法

      func prepareForReuse() {
        // code that resets state of your cell to default 
      }
      

      Apple Documentation中的方法说明

      【讨论】:

      • 嘿,我试过了,但是它更新了所有的单元格,无论如何我们可以指定单元格吗?
      • 占位符的问题很可能来自您的条件if chapterTitleTextView.text.isEmpty 检查您是否在 textView 上使用 becomeFirstResponder 以及是否在您将文本设置为 cellForRow 之前或之后调用它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多