【发布时间】:2019-09-19 07:11:30
【问题描述】:
我有一个集合视图,我正在为单元格使用自定义类。每个单元格是主视图的高度和宽度,每个单元格都有一个文本视图,代码如下:
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
}()
}
我的视图控制器上有一个按钮,我想要它做的是当我点击该按钮时,我希望我所有单元格中的文本都被打印出来。这可能吗?我已经做了我能做的,但它只打印文本视图包含的初始文本(“章节标题”),并且只打印可见单元格的文本。这是代码:
class ViewController: UIViewController {
@objc func printButtonTapped() {
let cv = CustomWriterPageCell()
print(cv.textViewOne.text)
// Prints- Chapter Title
}
}
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowlayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WriterPageCellID", for: indexPath) as! CustomWriterPageCell
cell.backgroundColor = indexPath.item % 2 == 1 ? .green : .yellow
return cell
}
}
是否可以一次打印所有单元格中的所有文本?
【问题讨论】:
-
你能出示你的cellforItem吗
标签: ios swift uicollectionview uicollectionviewcell