ios - 为什么我的 UITextView 没有显示在我的 Swift/iOS 应用程序的自定义表格单元格中?

标签 ios swift uitableview

我的自定义表格 View 单元格有一个标签和 TextView 。当我运行应用程序时,我只看到标签而不是 TextView 。

这是我的看法

enter image description here

这是我的 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
    }
}

这是我在运行应用程序时看到的内容:

enter image description here

我在界面生成器中正确设置了类,并且所有导出都是正确的。

这里是 textViews 约束

enter image description here

它显示 TextView 的位置、大小和可滚动内容大小不明确,标签的位置不明确,但我不明白为什么,因为我为 TextView 的所有四个边设置了约束,并为左上角设置了约束标签的一角。这还不够吗?

enter image description here

为什么 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/

相关文章:

ios - 美国东部时间 11 月 4 日至 5 日的夏令时问题

swift - uicelltableView 上的 uibutton 在 ios 9 上未单击,但在 ios 11 上可单击

ios - Self 类型的协议(protocol)函数

ios - 如何使用可滑动的 tableview 单元格实现与 iOS Mail 应用程序相同的功能?

ios - 无法将符合协议(protocol)的值设置为具有协议(protocol)类型的属性

ios - 在核心数据中存储 CLLocation 坐标

ios - 为什么 tableview 没有显示正确的图像?

swift - 在 CALayer 上过滤,除了形状是(不一定不同的)矩形的并集

ios - 将时区添加到日期选择器

ios - 创建弹出按钮界面