gpt4 book ai didi

iOS静态表格根据textView自动调整大小

转载 作者:搜寻专家 更新时间:2023-11-01 06:33:43 25 4
gpt4 key购买 nike

我在静态表中有 TextView 。我希望它们在需要换行时调整大小。我该怎么做呢?到目前为止,这是我的代码。

 override func viewDidLoad() {
super.viewDidLoad()

table.estimatedRowHeight = 40.0 // Replace with your actual estimation
table.rowHeight = UITableViewAutomaticDimension


// Tap to dismiss keyboard
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(EditInfoViewController.dismissKeyboard))
view.addGestureRecognizer(tap)


}


func dismissKeyboard() {
view.endEditing(true)
// Save data
}



func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

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

enter image description here

最佳答案

swift 3 和 Xcode 8.3.2

使用 UILabel 而不是 UITextView,并设置 numberOfLine = 0,因此它会根据其内容自动调整大小

如果你想保留 UITextView 而不是 UILabel,这里是代码

class YourClass: UITableViewController, UITextViewDelegate {

var yourCustomCell: UITableViewCell = UITableViewCell()

override func viewDidLoad() {
super.viewDidLoad()

table.estimatedRowHeight = 40.0 // Replace with your actual estimation
table.rowHeight = UITableViewAutomaticDimension


// Tap to dismiss keyboard
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(EditInfoViewController.dismissKeyboard))
view.addGestureRecognizer(tap)

// Add tableView delegate
tableView.dataSource = self
tableView.delegate = self

// Add textView delegate
yourTextView.delegate = self


}
// Text view delegate, dont forget to add yourTextView.delegate = self in viewDidLoad
func textViewDidChange(_ textView: UITextView) {
if textView == yourTextView {
let newHeight = yourCustomCell.frame.size.height + textView.contentSize.height
yourCustomCell.frame.size.height = newHeight
updateTableViewContentOffsetForTextView()
}
}
// Animate cell, the cell frame will follow textView content
func updateTableViewContentOffsetForTextView() {
let currentOffset = tableView.contentOffset
UIView.setAnimationsEnabled(false)
tableView.beginUpdates()
tableView.endUpdates()
UIView.setAnimationsEnabled(true)
tableView.setContentOffset(currentOffset, animated: false)
}


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



override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = yourCustomCell
cell.selectionStyle = .none
return cell
}
}

结果在这里: Before the code

使用 textViewDelegate 和自定义调整大小功能后的结果 After the code

关于iOS静态表格根据textView自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44207519/

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