gpt4 book ai didi

ios - dequeueReusableCell 导致文本改变颜色

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

我有一个 Collection View ,我正在为单元格使用一个自定义类。每个单元格都有两个 TextView ,chapterTitleTextView 和 chapterBodyTextView。我已将占位符添加到两个 TextView 中,如下所示:

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

}
}
}

这是如何工作的,文本颜色最初是灰色的,并且有一些文本,当用户点击 TextView 时,颜色变为黑色, TextView 中的文本被删除。

现在使用 dequeueReusableCell 存在这个问题,它重用了单元格,这导致了问题编号 1:无论我在第一个单元格中的 TextView 中键入什么,都会出现在第四个单元格中,为了解决这个问题,我必须创建2 个全局列表来保存我输入的任何内容,这会显示在单元格的 TextView 中,这是代码:

全局列表:

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

以下代码片段位于之前的 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( TextView 中的文本重复)。但是后来我遇到了一个新问题,还记得我说的占位符文本吗?当我在第一个单元格的 TextView 中输入内容时,第四个单元格中 TextView 的文本颜色会发生变化。

这是复制问题的 GIF:

Video replicating the problem

最佳答案

根据您的一系列问题,我建议您每次处理 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])

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

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

关于ios - dequeueReusableCell 导致文本改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58038770/

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