Here is the set up:
a custom cell, with a single item in the content view : a UITextView.
class CustomeCell: UITableViewCell
@IBOutlet weak var aTextView: UITextView!with its nib
It displays well, I can type text inside.
But I cannot find a way to get in a var the text that was typed
Here is what I try :
var toGetText: String
guard let aCell = aCell as? CustomeCell else { return }
toGetText = aCell.aTextView.text ?? "" // I get there, but text is empty or niltoGetText remains empty (does not get what was typed in)
I tried to print several variations:
print(aCell.aTextView.text, "Attributed", aCell.aTextView.attributedText, "Storage: ", aCell.aTextView.textStorage.string,"Storage attributed: ", (aCell.aTextView.textStorage as NSAttributedString).string)just get empty content
Optional("") Attributed Optional() Storage: Storage attributed:
So I am misusing UITextView, but cannot find the correct use.
That makes me think that the cell reference is correct.
I really doubt that. As seeing this line:
let aCell = tableView(actionsTableView, cellForRowAt: indexPath)You usually do not call `tableView(_:cellForRowAt:)` directly. It's for the iOS system to get a populated cell which is getting visible from now on.
You may get the right cell (it's reused) incidentally, but I believe you should not rely on such a luck.
This is not the recommended way, but change the line as below and see what you get.
let aCell = actionsTableView.cellForRow(at: indexPath)Generally, you should better not consider view objects as storage. When you have editable fields in your cell, you should reflect the content to your model object immediately.
EDITED
You have no need to touch textStorage of a UITextView, unless you want to control the editing behavior yourself. Just use the property `text` or `attributedText`.