gpt4 book ai didi

ios - 如何从主类访问自定义 Collection View 单元格类中的 TextView

转载 作者:行者123 更新时间:2023-11-28 11:26:15 27 4
gpt4 key购买 nike

我的每个 Collection View 单元格中都有两个 TextView ,我为这些单元格创建了一个自定义类。这是代码:

class CustomWriterPageCell: UICollectionViewCell {

fileprivate let textViewOne: UITextView = {

let tv = UITextView()
tv.backgroundColor = .cyan
tv.text = "Chapter Title"
tv.font = UIFont(name: "Avenir-Roman", size: 27)
tv.textColor = .gray
return tv

}()

fileprivate let textViewTwo: UITextView = {

let tv = UITextView()
tv.textColor = .gray
tv.text = "Start your story..."
tv.textContainerInset = UIEdgeInsets(top: 20, left: 15, bottom: 20, right: 15)
tv.font = UIFont(name: "Avenir-Book", size: 23)
tv.backgroundColor = .black
return tv

}()
}

我想为这两个 TextView 添加占位符,但问题是,由于它们在自定义类中,据我所知,无法找出正在编辑的 TextView ,所以我不能在 textViewDidEndEditing 发生时添加相应的占位符,有没有办法找出正在编辑的 TextView ?有没有办法从主类访问 TextView ?

最佳答案

在您的 CustomWriterPageCell 类中:

fileprivate let textViewOne: UITextView = {

let tv = UITextView()
tv.backgroundColor = .cyan
tv.text = "Chapter Title"
tv.font = UIFont(name: "Avenir-Roman", size: 27)
tv.textColor = .gray
return tv

}()

fileprivate let textViewTwo: UITextView = {

let tv = UITextView()
tv.textColor = .gray
tv.text = "Start your story..."
tv.textContainerInset = UIEdgeInsets(top: 20, left: 15, bottom: 20, right: 15)
tv.font = UIFont(name: "Avenir-Book", size: 23)
tv.backgroundColor = .black
return tv

}()

override init(frame: CGRect) {
super.init(frame: frame)

textViewOne.delegate = self
textViewTwo.delegate = self

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

然后:

extension CustomWriterPageCell : UITextViewDelegate {
func textViewDidEndEditing(_ textView: UITextView) {

if textView == textViewOne {
textView.text = "..."
}
else if textView == textViewTwo {
textView.text = "..."
}
}
}

这样,您的 View Controller 仍然不需要知道您的 TextView 。

关于ios - 如何从主类访问自定义 Collection View 单元格类中的 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57977396/

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