2

I m doing an iOS project using swift 2, I have a UITableView with custom cells. 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. 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

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) } } 

4 Answers 4

1

You need to add answerField.delegate = self in awakeFromNib to receive delegate callbacks.

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:

class ParagraphCell: UITableViewCell, UITextViewDelegate { } 
Sign up to request clarification or add additional context in comments.

4 Comments

If I do that I have this error : cannot assign value of type ParagraphCell to type UITextViewDelegate?
You need to add UITextViewDelegate to the ParagraphCell protocol declaration
class ParagraphCell: UITableViewCell, UITextViewDelegate {
Ohhhh Waww, I never saw this notation before ! it solved the problem thanks a lot my friend ! :)
1

Yes, you need to set the delegate of the UITextView to the object where you want to put your textViewDidChange method. 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.

Comments

1
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 

Comments

1

you should use didEndEditing delegate method which is getting called after editing will complete.

You can set tag to textview as indexpath.row means row number to differentiate the textview in delegate method.

Or you can implement delegate method in custom tableviewcell class.

Better way is to implement delegate method in viewcontroller and set tag to textview in cellforrowatindexpath.

hope this will help :)

1 Comment

textViewDidEndEditing getting called when you finish editing of textview. like if you click on other controls or you switch to other controls from textview or you resign keyboard etc

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.