My custom tableview cell has a label and textview. When I run the app I only see the label and not the textview.
我的自定义tableview单元格有标签和textview。当我运行应用程序时,我只看到标签,而不是textview。
Here is my View
这是我的观点
Here is my ViewController:
这是我的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:
这是我在运行应用程序时看到的内容:
I have the classes set correctly in the interface builder and all the outlets are correct.
我在界面构建器中正确设置了类,并且所有插座都是正确的。
Here are the textViews constraints
这是textViews约束
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?
它显示的位置,大小和可滚动内容大小对于textview是不明确的,并且位置对于标签是模糊的,但是我不明白为什么因为我为textview的所有四个边设置了约束并且为左上角的约束设置了约束。标签。应该不够吗?
Why is the TextView not showing?
为什么TextView没有显示?
How do I get it to show with a paragraph of text?
如何使用一段文字显示它?
1 个解决方案
#1
1
first set your textView width fixed (i.e 100 or 150)
首先将textView宽度设置为固定(即100或150)
add protocol :
添加协议:
UITextViewDelegate
at cellForRow at IndexPath mehod add
在indexPath mehod添加的cellForRow
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
}
#1
1
first set your textView width fixed (i.e 100 or 150)
首先将textView宽度设置为固定(即100或150)
add protocol :
添加协议:
UITextViewDelegate
at cellForRow at IndexPath mehod add
在indexPath mehod添加的cellForRow
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
}