tableview 内的 UITextView 未根据内容调整大小

UITextView inside tableview not resizing according to content

我有一个 tableView 和一个 UITextView 里面。我正在尝试将数据从 JSON 添加到 table 视图,但根据大小它不是 shrinking/expanding。我尝试了很多论坛和方法。

主要问题是 - 如果它以 expanding/shrinking 高度开始,它会停止 shrinking/expanding,反之亦然。机器人高度和宽度不起作用。

这是因为,当我将 UITextView 的尾部和前导约束设置到单元格时,它开始处理高度但停止处理宽度。当我删除 leading/trailing 约束时,UITextView 的内容超出屏幕并且不会以多行形式出现,即不随高度扩展。 我试过 -

How do I size a UITextView to its content?

How to resize table cell based on textview?

还有很多这样的。

我的一点代码-

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Message") as! TextViewCell
    cell.customTextView.textColor = UIColor.white

    // Give a source to table view cell's label
    cell.customTextView.text = requestResponseArr[indexPath.row]
    cell.translatesAutoresizingMaskIntoConstraints = true
    cell.sizeToFit()        

    if (indexPath.row % 2 == 0) {
        cell.customTextView.backgroundColor = ConstantsChatBot.Colors.iMessageGreen
        cell.customTextView.textAlignment = .right
    } else {
        cell.customTextView.backgroundColor = ConstantsChatBot.Colors.ButtonBlueColor
        cell.customTextView.textAlignment = .left
    }

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

并且在 viewDidLoad()-

    override func viewDidLoad() {
    // RandomEstimatedRowHeight
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 300
}

我不希望 textview 是 editable。请用 swift 回复,因为我不熟悉 Objective C。谢谢

编辑:自定义单元格代码

class TextViewCell: UITableViewCell {
@IBOutlet weak var customTextView: UITextView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    customTextView.isScrollEnabled = false
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
}

}

为此您需要禁用 UItextView 滚动。

textView.isScrollingEnabled = false

并在单元格中添加textview的上下左右约束。 并添加高度约束 >= 10

禁用 UITextView isScrollEnabledfalsecellForRowAt

里面

Demo Example

您需要为 textview 设置所有四个约束,即前导、尾随、顶部和底部假设所有都设置为距边距 8。

它将如下所示:

查看 GitHub here

上的演示

我试过在 UITableViewCell 中使用 UITextView

约束条件

UITextView,顶部、底部和右侧为 2、2 和 5,宽度为 50。Font Size 为 13,Alignment 为中心。将 Outlet connection 作为 Width constraints 作为 txtVwWidthConst

UIViewController

@IBOutlet weak var tblView: UITableView!
var textWidthHeightDict = [Int : CGSize]()

override func viewDidAppear(_ animated: Bool) {

    stringValue = [0 : "qwer\nasdasfasdf", 1 : "qwe", 2 : "123123\nasdas\nwqe", 3 : "q\n3\n4", 4 : "klsdfjlsdhfjkhdjkshfjadhskfjhdjksfhkdjsahfjksdhfkhsdfhjksdfjkasklsdfjlsdhfjkhdjkshfjadhskfjhdjksfhkdjsahfjksdhfkhsdfhjksdfjkasklsdfjlsdhfjkhdjkshfjadhskfjhdjksfhkdjsahfjksdhfkhsdfhjksdfjkas"]

    for i in 0..<stringValue.count
    {
        GettingTextViewSize(getStr: stringValue[i]!, fontSize: 14, loopValue: i)
    }
    tblView.reloadData()
}

func GettingTextViewSize(getStr : String, fontSize: CGFloat, loopValue : Int)
{
    var textSize = (getStr as! NSString).size(withAttributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: fontSize)])
    if textSize.width > self.tblView.frame.width
    {
        // IF STRING WIDTH GREATER THAN TABLEVIEW WIDTH
        let multipleValue = textSize.width / self.tblView.frame.width
        textSize.height = (textSize.height * (multipleValue + 1.0))
        textSize.width = self.tblView.frame.width

        textWidthHeightDict[loopValue] = textSize
    }
    else
    {
        textSize.height = textSize.height + 10 //ADDING EXTRA SPACE
        textSize.width = textSize.width + 10 //ADDING EXTRA SPACE

        textWidthHeightDict[loopValue] = textSize
    }
}


func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

    return stringValue.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "table", for: indexPath) as! TblTableViewCell

    cell.backgroundColor = cellBGColr[indexPath.row]

    cell.txtVw.inputAccessoryView = toolBar
    cell.txtVw.text = stringValue[indexPath.row]
    cell.txtVw.tag = indexPath.row
    cell.txtVwWidthConst.constant = (textWidthHeightDict[indexPath.row]?.width)!
    cell.selectionStyle = .none
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var heightVal = (textWidthHeightDict[indexPath.row])
    return heightVal!.height + 8 //ADDING EXTRA SPACE
}

UITableViewCell

class TblTableViewCell: UITableViewCell {
    @IBOutlet weak var txtVw: UITextView!
    @IBOutlet weak var txtVwWidthConst: NSLayoutConstraint!
    // TEXTVIEW WIDTH CONSTRAINTS    


    override func awakeFromNib() {
       super.awakeFromNib()
    }
}

输出

备注

UITextView 而已,我们必须计算字符串大小。但是,在UILabel中,这个概念很简单。如果您有任何疑问,请告诉我。