skip to Main Content

I struggle a bit to find or think of a way that I can store the textView text.
so later I can pass it back to the server if needed.

I have a TextView Inside a CustomCell UITableViewCell. (I have 2 sections inside my tableview and in each section there is that custom cell with a textView inside.)
I need to somehow pass or save the text that the user have written so I could pull that text and send it somewhere else.. and each time the text inside my "reusable" cell gets reused the text disappears.
I have tried to save it inside my customCell Swift but I don’t think that’s a good idea to save the text inside a customCell file.
any one have idea?
thank you!

2

Answers


  1. While implementing tableView, we should make sure to have some data source that is data driven. What I mean here is to create a model that drives your tableView’s numberOfSections, numberOfRowsInSection etc. This way you can have a property in your model to store textView’s text.

    Sample Model:

        struct TableViewModel {
        // this will handle number of sections
        var sections: [TableViewSectionModel]
    }
    
    struct TableViewSectionModel {
        // this will handle number of rows
        var rows: [TableViewRowModel]
    }
    
    struct TableViewRowModel {
        var textViewText: String
        /*
         Other properties to fill in
         */
    }
    

    Using this you can create your dataModel like this:

    var cell1 = TableViewRowModel(textViewText: "")
    var cell2 = TableViewRowModel(textViewText: "")
    
    var section1 = TableViewSectionModel(rows: [cell1, cell2])
    var section2 = TableViewSectionModel(rows: [cell1, cell2])
    
    var data = TableViewModel(sections: [section1, section2])
    

    This will help to update property in model and you can have your data to pass it to backend later.

    Login or Signup to reply.
  2. First step:
    Create a model to populate the cells.

    struct myModel {
    let textFromCustomCell: string = ""
    }
    

    Create a list of models (to populate the tableview)

    Second step:
    This has several way to do so but i chose the delegate approach.

    1. Make your CustomCell conform to UITextViewDelegate.

    2. Save a indexPath var on the CustomCell:

      var indexPath: IndexPath?

    3. Implement this UITextview Delegate method on the cell:

      func textViewDidChange(textView: UITextView)

    4. Set the textView delegate in the cell awakeFromNib:

       override func awakeFromNib() {
      super.awakeFromNib()
      textField.delegate = self 
      

      }

    5.Create a CustomCellDelegate:

    protocol CustomCellDelegate: AnyObject {
        func cellTextDidChanges(indexPath: IndexPath, text: String)
    }
    
    1. Call this function on the cell when needed (probably inside textViewDidChange function implementation on the cell).

    2. Make the viewControlller that holds the tableview to conform to your CustomCellDelegate

    3. Implement the CustomCellDelegate cellTextDidChanges method on your viewControlller

    4. On the cellTextDidChanges implementation you should update the correct model text on the model list you created according to the indexPath.

    Remember when the tableview will be populated for the first time the text will be empty.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search