How to embed UITextView inside UITableViewCell with custom cell that's the delegate?

get text from uitextfield in uitableviewcell swift 4
get textfield value from uitableviewcell swift
get value uitextfield uitableviewcell
add textfield to uitableviewcell programmatically swift
get textfield value from uitableviewcell objective-c
uitextview in uicollectionviewcell
uitableview inside uitableviewcell example swift
scroll uitableview when keyboard appears swift

I've got a UITableViewController with two custom cells - one contains a UITextField (for the user to input a title) and the other contains a UITextView (for the user to input a description). Whenever these change, I want to update my memory object (which is a struct with two variables - memoryTitle and memoryDescription).

The memoryTitle seems simple enough - on my ViewController I have the following:

 @IBAction func memoryTitleChanged(_ sender: UITextField) {
        memory.memoryTitle = sender.text ?? ""
    }

The UITextView has confused me slightly though. There's two issues I'm having - I can't create an action in the same way I can for the UITextField, so my next thought was to make the ViewController the delegate and use textViewDidChange to update memory.memoryDescription but that brings me to my second problem.

In order to make the UITextView cell resize dynamically, I used the following tutorial which works perfectly (https://medium.com/@georgetsifrikas/embedding-uitextview-inside-uitableviewcell-9a28794daf01) to make my custom cell this:

class DetailTextTableViewCell: UITableViewCell, UITextViewDelegate {

    //Found below method for resizing UITextView and cell - https://medium.com/@georgetsifrikas/embedding-uitextview-inside-uitableviewcell-9a28794daf01
    @IBOutlet weak var memoryDescriptionTextView: UITextView!
    var textChanged: ((String) -> Void)?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        memoryDescriptionTextView.delegate = self
        memoryDescriptionTextView.backgroundColor = UIColor.red
    }

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

    //Found below method for resizing UITextView and cell - https://medium.com/@georgetsifrikas/embedding-uitextview-inside-uitableviewcell-9a28794daf01
    func textChanged(action: @escaping (String) -> Void) {
        self.textChanged = action
    }

    func textViewDidChange(_ textView: UITextView) {
        textChanged?(textView.text)
    }
}

Now I'm stuck with DetailTextTableViewCell being the UITextView's delegate, so I'm not sure how to make it update my memory object in the ViewController when the text changes. If anyone has any ideas or guidance it'd be much appreciated!

Thank you in advance.

declare this protocol above your cell DetailTextTableViewCell

protocol CellDelegate {
  func textViewChanged(textView : UITextView)
}

add a delegate var in your DetailTextTableViewCell

var delegate : CellDelegate?

In the cell for row of your tableView assign self to delegate property of cell

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
cell.delegate = self

}

In your DetailTextTableViewCell add this line inside textViewDidChange

 func textViewDidChange(_ textView: UITextView) {
    textChanged?(textView.text)
    delegate?.textViewChanged(textView)
}

Now implement the delegate function in your view controller

func textViewChanged(textView: UITextView) {

    }

How to embed UITextView inside UITableViewCell with custom cell , declare this protocol above your cell DetailTextTableViewCell protocol CellDelegate { func textViewChanged(textView : UITextView) }. add a� yes it is possible, I added the UITableVIew within the UITableView cell .. :) no need to add tableview cell in xib file - just subclass the UITableviewCell and use the code below, a cell will be created programatically.

First, you don't need textChanged method

func textChanged(action: @escaping (String) -> Void) {


Then, what you need, is assigning your textChanged closure variable (which is good approach btw) in controller's cellForRowAt for each certain cell.

Inside closure declare, that when text view did change, certain item's (from table view data source array) property will be assigned with String parameter of closure and if you need, then reload certain cell for this IndexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    ...
    cell.textChanged = { text in
        self.dataSourceArray[indexPath.row].stringProperty = text
        // tableView.reloadRows(at: [indexPath], with: .none) 
            // if you want to reload row, move calling closure 
            // to `textViewDidEndEditing` instead
    }
    ...
}

Swift / how to use UITextView with dynamic height INSIDE , Self-sizing UITextView in a UITableView Create a Custom cell with name GrowingCell, add UITextView in the cell and create an Outlet of this. The custom cell is designed in a .xib file and has its own class called cell, which is a subclass of UITableViewCell: Cell.m #import "Cell.h" @implementation Cell - (void)awakeFromNib { // Ini How to properly initialize a custom UITableviewCell?

Inside cellForRowAt do

let cell = /// 
cell.memoryDescriptionTextView.tag = indexPath.row
cell.memoryDescriptionTextView.delegate = self

and implement the delegate methods inside the vc

The dynamic height of UITextView inside UITableViewCell Swift, Add UITextView in Cell. Now next step is to add a UITextView in Cell and perform below changes. 1. Select UITextView 2� I'm trying to set the text of a UITextView that's inside of a custom UITableViewCell in a load function that's called on the cell when it's contents are being rendered in its table view. These are my current settings on my textview that's inside my custom tableviewcell. Note that UserInteraction is NOT enabled, but Editable and Selectable ARE.

Try to enable user interaction property of text view inside cellForRowAt method.

cell.memoryDescriptionTextView.delegate = self
cell.memoryDescriptionTextView.isUserInteractionEnabled = true

UITextView inside custom UITableViewCell, I am currently working on app where I have tableView with custom cells which have if user clicks anywhere else than link (textView or any other place in cell), inside your table cell and set the TitleText of the button to display your link. Customize the table view cell – instead of using the default style of table view cell, we’ll take a look how to build our own. Display Different Thumbnails Before we move on to change the code, let’s revisit the code for displaying thumbnail in table row.

Autoresizing UITextView inside of UITableViewController using Swift , Short tutorial from Wolfpack Digital showing how to create an automatically autoresizing Duration: 5:25 Posted: Aug 6, 2016 Following from the previous tutorial on putting a like image in a custom UITableViewCell, in this tutorial we will change that UIImage for a UIButton, and then detect which button has been pressed

Resize UITableViewCell containing UITextView upon typing, I have an UITableViewController that contains a custom cell. Each cell was Embedding UITextView inside UITableViewCell, After all it just a UITextView in a � Custom TableView Cell - Part 26 - Itinerary App (iOS, Xcode 10, Swift 4) - Duration: 25:32. Mark Moeykens 3,039 views. 25:32. IOS 11, Swift 4, Tutorial : How To Create Custom Cell Tableview ( with

How to get all custom tableview cell textField and textView value , Embedding UITextView inside UITableViewCell, I tried to access custom tableView cell textfield from outside tableView like this : for i in 0..<NameCounter { let� I have two UITableViews using Storyboards in Xcode 7. I've set the delegate and dataSource using the Connections Inspector for both table views. Let the first table view be the main table view and

Comments
  • from the code it seems like you have a closure textChanged which will be invoked on the text changes. So I believe you will be assigning value to it on cellForRow method, something like cell.textChanged {}. Use this closure to set value to memory.memoryDescription
  • Please don’t do this. If he already has closure variable, why should he create delegate protocol for this?
  • hi, @RobertDresler is it necessary to reload cell? because the user has already typed in UItextview and it is already can show typed text then why need to reload cell?
  • @AtulParmar oh, okay, It's not necessary.