ios - tableview 内的 UITextView 不根据内容调整大小

标签 ios swift uitableview uitextview

我里面有一个 tableView 和一个 UITextView 。我正在尝试将 JSON 中的数据添加到 TableView 中,但它不会根据大小缩小/扩展。我尝试了很多论坛和方法。

主要问题是 - 如果它开始随高度扩展/收缩,它就会停止收缩/扩展,反之亦然。机器人高度和宽度不起作用。

这是因为,当我将 UITextView 的尾随和前导约束设置到单元格时,它开始使用高度,但停止使用宽度。当我删除前导/尾随约束时,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 可编辑。由于我对 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
}

}

最佳答案

我尝试过在UITableViewCell内使用UITextView

约束

UITextView,上、下、右为 2、2 和 5,宽度为 50。字体大小为 13,对齐方式为居中。将宽度约束 socket 连接指定为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()
    }
}

输出

enter image description here

注意

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

关于ios - tableview 内的 UITextView 不根据内容调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49291552/

相关文章:

ios - 在本地化的 xib 文件中无法识别非本地化图像

ios - 通用寄存器的内容包含什么?

ios - 为什么这个空白游戏在 Swift 中有漏洞

swift - 从 Swift 2.3 迁移到 Swift 3 错误消息

ios - 什么样的 ScrollView 包含2个 TableView 和 Collection View

ios - UITableViewController 页脚内的自定义 UITableView Cell

ios - 在 UIGraphicsBeginImageContext 中绘制时 UIImage 方向发生变化

ios - 处理从 QR 码读取的数据

iphone - uitableview 单元格中的自定义删除按钮不起作用

ios - TableView 单元格出队后填充的嵌入式 Collection View 的动态 TableView 单元格高度