我的自定义表格 View 单元格有一个标签和 TextView 。当我运行应用程序时,我只看到标签而不是 TextView 。
这是我的看法
这是我的 View Controller :
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
}
}
这是我在运行应用程序时看到的内容:
我在界面生成器中正确设置了类,并且所有导出都是正确的。
这里是 textViews 约束
它显示 TextView 的位置、大小和可滚动内容大小不明确,标签的位置不明确,但我不明白为什么,因为我为 TextView 的所有四个边设置了约束,并为左上角设置了约束标签的一角。这还不够吗?
为什么 TextView 不显示?
如何让它与一段文字一起显示?
最佳答案
首先将您的 textView 宽度设置为固定值(即 100 或 150)
添加协议(protocol):
UITextViewDelegate
在 cellForRow 和 IndexPath 方法中添加
cell.textviewDemo.delegate = self
添加委托(delegate)方法按回车结束编辑
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text == "\n")
{
self.view.endEditing(true);
return false;
}
return true
}
关于ios - 为什么我的 UITextView 没有显示在我的 Swift/iOS 应用程序的自定义表格单元格中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44642872/