简体   繁体   English

从UITextView Swift 2获取文本

[英]Get text from UITextView Swift 2

I m doing an iOS project using swift 2, I have a UITableView with custom cells. 我正在使用swift 2做一个iOS项目,我有一个带有自定义单元格的UITableView。 I have one cell with a UITextView and a simple label, and I wan't to get the value of this UITextView when it's modified, but it's not like UITextFields, I can't use on edit begin method. 我有一个带有UITextView和简单标签的单元格,修改后我不希望获得此UITextView的值,但它与UITextFields不同,我不能在edit begin方法上使用。 How can I do that ? 我怎样才能做到这一点 ?

Here is my code to define my custom cell : 这是定义我的自定义单元格的代码:

let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.ParagraphCell, forIndexPath: indexPath) as! ParagraphCell
                cell.display(block)
                cell.selectionStyle = UITableViewCellSelectionStyle.None
                tableView.rowHeight = UITableViewAutomaticDimension
                tableView.estimatedRowHeight = 160.0
                return cell

And here is my ParagraphCell.swift 这是我的ParagraphCell.swift

import UIKit

protocol ParagraphCell {
    func update ParagraphCell(cell: ParagraphCell, rep: String)
}

class ParagraphCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var answerField: UITextView!


    var delegate: ParagraphCell Delegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        let borderColor : UIColor = UIColor(red: 0.85, green: 0.85, blue: 0.85, alpha: 1.0)
        answerField.layer.borderWidth = 0.5
        answerField.layer.borderColor = borderColor.CGColor
        answerField.layer.cornerRadius = 5.0

    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func display(block: Block){
        titleLabel.text = block.title
        answerField.text = ""
    }

//THIS METHOD IS NEVER CALLED... 
    func textViewDidChange(textView: UITextView) { //Handle the text changes here
        print(textView.text); //the textView parameter is the textView where text was changed
        delegate?.update ParagraphCell(self, rep: textView.text)
    }


}

You need to add answerField.delegate = self in awakeFromNib to receive delegate callbacks. 您需要在awakeFromNib中添加answerField.delegate = self以接收委托回调。

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    let borderColor : UIColor = UIColor(red: 0.85, green: 0.85, blue: 0.85, alpha: 1.0)
    answerField.layer.borderWidth = 0.5
    answerField.layer.borderColor = borderColor.CGColor
    answerField.layer.cornerRadius = 5.0
    answerField.delegate = self // create delegate reference
}

You'll also need to add UITextViewDelegate to your class protocol declaration: 您还需要将UITextViewDelegate添加到类协议声明中:

class ParagraphCell: UITableViewCell, UITextViewDelegate { }

Yes, you need to set the delegate of the UITextView to the object where you want to put your textViewDidChange method. 是的,您需要将UITextView的委托设置为要放置textViewDidChange方法的对象。 At the moment you have it in the ParagraphCell class, but it would probably make more sense to put this into your view controller instead, and make the view controller the delegate. 目前,您已经在ParagraphCell类中拥有了它,但是将其放到视图控制器中,然后将视图控制器作为委托可能更有意义。

let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.ParagraphCell, forIndexPath: indexPath) as! ParagraphCell
                cell.display(block)
                cell.selectionStyle = UITableViewCellSelectionStyle.None
                cell.answerField.delegate =  self
                tableView.rowHeight = UITableViewAutomaticDimension
                tableView.estimatedRowHeight = 160.0
                return cell

you should use didEndEditing delegate method which is getting called after editing will complete. 您应该使用didEndEditing委托方法,该方法将在编辑完成后调用。

You can set tag to textview as indexpath.row means row number to differentiate the textview in delegate method. 您可以将标签设置为textview,作为indexpath.row表示在委托方法中使用行号来区分textview。

Or you can implement delegate method in custom tableviewcell class. 或者,您可以在自定义tableviewcell类中实现委托方法。

Better way is to implement delegate method in viewcontroller and set tag to textview in cellforrowatindexpath . 更好的方法是在viewcontroller实现委托方法,并在cellforrowatindexpath中将tag设置为textview

hope this will help :) 希望这会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM