gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:03:52 26 4
gpt4 key购买 nike

我里面有一个 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 不熟悉,请 swift 回复。谢谢

编辑:自定义单元格代码

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,top、bottom、right 为 2、2 和 5,Width 为 50。Font Size 为 13,Alignment 为 Center .为 Width constraints 提供 Outlet connection 作为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com