Matthew Drill Matthew Drill - 3 days ago 3
iOS Question

Why is my UITextView not showing in my Swift/iOS app's custom table cell?

My custom tableview cell has a label and textview. When I run the app I only see the label and not the textview.

Here is my View

enter image description here

Here is my ViewController:

class PostTableViewCell: UITableViewCell {
@IBOutlet weak var authorAndTimeLabel: UILabel!
@IBOutlet weak var textTextView: UITextView!
}

class WallViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var postTableView: UITableView!

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postcell", for: indexPath) as! PostTableViewCell
cell.authorAndTimeLabel?.text = "foo"
cell.textTextView?.text = "bar"

return cell
}
}


Here is what I see when I run the app:

enter image description here

I have the classes set correctly in the interface builder and all the outlets are correct.

Here are the textViews constraints

enter image description here

It's showing position, size, and scrollable content size are ambiguous for the textview and position is ambiguous for the label, but I don't understand why since I set constraints for all four sides of the textview and constraints for the top left corner of the label. Shouldn't that be enough?

enter image description here

Why is the TextView not showing?

How do I get it to show with a paragraph of text?

Tan
Answer

first fixed your textView width fixed (i.e 100 or 150)

add protocol :

 UITextViewDelegate

at cellForRow at IndexPath mehod add

  cell.textviewDemo.delegate = self

add the delegate method to end editing when press return

 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if (text == "\n")
    {

        self.view.endEditing(true);
        return false;
    }
    return true
}