Ios 一次从所有集合视图单元格内的文本视图获取文本

Ios 一次从所有集合视图单元格内的文本视图获取文本,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我有一个集合视图,我正在为单元格使用一个自定义类。每个单元格都是主视图的高度和宽度,每个单元格都有一个文本视图,下面是代码: class CustomWriterPageCell: UICollectionViewCell { fileprivate let textViewOne: UITextView = { let tv = UITextView() tv.backgroundColor = .cyan tv.text = "Ch

我有一个集合视图,我正在为单元格使用一个自定义类。每个单元格都是主视图的高度和宽度,每个单元格都有一个文本视图,下面是代码:

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
    }

}
我的视图控制器上有一个按钮,我想让它做的是,当我点击这个按钮时,我想打印我所有单元格中的文本。这可能吗?我已经尽力了,但它只打印文本视图包含的初始文本章节标题,并且只打印可见单元格的文本。代码如下:

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
    }

}

是否可以一次打印所有单元格中的所有文本?

如果您已经设置了数据源,并且单元格上显示了所有文本的数组,则可以通过访问数据本身一次打印所有文本,如下所示:-



class ViewController: UIViewController {

   fileprivate var texts = [String]()

    @objc func printButtonTapped() {
       texts.forEach { (text) in
                      print(text)
                    }
    }
}




例如,您需要创建一个模型

struct YourModel {
    var content = "" // Content is what's displayed in your cell
}
然后,您还需要一个阵列来管理控制器中的模型

class ViewController: UIViewController {

    var yourModels = [YourModel]()
}
您的模型将用于在tableView中显示的数组

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CustomWriterPageCell, self.yourModels.count > indexPath.row else {
            return UITableViewCell()
        }
        cell.config(byYourModel: self.yourModels[indexPath.row])
        return cell
}
配置你的手机

 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

        }()

        func config(byYourModel model: YourModel) {
            self.textView.text = model.content
        }
    }
然后如果你想打印所有

class ViewController: UIViewController {

    @objc func printButtonTapped() {

        for model in self.yourModels {
            print(model.content)
        }
    }
}
我从你的生日起就记得你。对我先前的答案稍作修改将对你有所帮助。在上一个问题中,您将CustomWriterPageCell设置为UITextView的委托。这次,将控制器设置为委托,而不是那样

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
    cell.textViewOne.delegate = self

    return cell
}
现在,遵守协议:

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

      let text = textView.text 
  }
}

到目前为止,您已成功地将单元格中的文本视图中的文本传输到控制器。 现在的问题是,您不知道这个文本属于哪个索引XPath

我并不是说这是最好的解决方案,但在您的情况下,它是安全的: 您可以为每个TextView设置一个标签,其作用类似于id:

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
    cell.textViewOne.delegate = self
    cell.textViewOne.tag = indexPath.row

    return cell
}

然后:

您可以在控制器中这样声明: var text=Arrayrepeating:,count:10,其中10是集合视图中的项数

一切都设置好了,让我们进入打印按钮:

 @objc func printButtonTapped() {

       for text in self.texts {

            print(text)

       }

}

你能展示你的CellForItem吗?嘿,我说的是集合视图,而不是表格视图,和集合视图一样,你只需要一个数组来管理你的模型。然后,您可以对这些模型执行任何操作,例如:打印出本例的值这行的替换内容是什么,这会给出一个错误:cell.configbyYourModel:self.yourModels[indexPath.row]您遇到了什么错误?请注意,您可以根据自己的风格使用不同的类、函数名,我的只是示例。“CustomWriterPageCell”类型的值没有成员“config”嘿,我没有数组设置,您能告诉我怎么做吗,对不起,我不太擅长swiftHey,非常感谢您的回答,它可以工作,但现在问题是,我在上一个问题中想要的东西停止工作了,你可以用任何方式使两者都起作用吗?是的,我可以使两者都起作用;我会在完成我的东西后编辑我的答案。嘿,多亏了你,我才弄明白。你就是那个人!还有,如果你不介意我问的话,你是怎么学会斯威夫特的?课程还是???不客气。很高兴你来了。我从看马克·普莱斯的视频开始。他们很棒。嘿,你知道如何解决细胞复制问题吗?这是因为dequeuReuseableCell发生的,但我似乎无法理解