Afshin Mobayen Khiabani
Afshin Mobayen Khiabani

Reputation: 1269

Get UITextView data in custom cell in swift

I have a custom cell with a UITextView in it and outlet for that UITextView is in the customCell file.

Now in my viewController I have tableView and I create and add that custom cell into my table. After this I can't access UITextView and get its data as its outlet in the customCell file. I can't move the outlet to my viewController as I need it there. How can I do this (access the UITextView in my viewController)?

Upvotes: 1

Views: 1711

Answers (2)

Nirav D
Nirav D

Reputation: 72410

You can set that delegate of cell's textView with your viewController, First set its delegate inside your cellForRowAtIndexPath

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomTableCell 
    cell.textView.delegate = self
    cell.textView.tag = indexPath.row //In case you have multiple textView
    return cell
}

Now you can get this textView inside its UITextViewDelegate method.

func textViewDidBeginEditing(textView: UITextView) {
    print(textView.text)
} 

Upvotes: 7

Santosh
Santosh

Reputation: 2914

You need to declare a local variable in your viewController to get the data of your UITextView. Then implement the tableViews delegate method

 //this is your local variable declared in viewCOntroller
 var myTextViewText: String?

//And this is your delegate method
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! YourCell
    myTextViewText = cell.textView.text //here you will get the text of your cell's textView
}

Upvotes: 0

Related Questions