Afshin M. Khiabani Afshin M. Khiabani - 3 months ago 17
iOS Question

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

Answer

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