swift - 如何使用 Swift 创建 TableView 单元格 TextView 的动态高度?

标签 swift

我正在创建带有顶部和底部两个 TextView 的单个Tableview自定义cell行。这里,底部textview需要根据textview文本长度动态调整height

我的 Storyboard

enter image description here

override func viewDidLoad() {
   super.viewDidLoad()
   self.tableView.rowHeight = 280.0
}

//Choose your custom row height
private func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
   return 280.0;
}

// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return self.animals.count
}

// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
    cell.sourceTextView.text = “ABCD….”
    return cell
}

最佳答案

就这样做

func adjustUITextViewHeight(arg : UITextView)
{
arg.translatesAutoresizingMaskIntoConstraints = true
arg.sizeToFit()
arg.scrollEnabled = false
}

在 Swift 4 中,arg.scrollEnabled = false 的语法已更改为

arg.isScrollEnabled = false.

关于swift - 如何使用 Swift 创建 TableView 单元格 TextView 的动态高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56226152/

相关文章:

ios - 如何确保我的应用程序不会发出太多通知

swift - 在 Swift 中测试空 Objective-C 字符串的正确方法?

swift - Swift 中 "optional chaining"和 "optional call chaining"之间的区别

ios - 扩展中的延迟加载属性(Swift)

ios - nib/xib 文件是否适用于不同的 View Controller ?

swift - 设置指向用 Swift 解析的指针

swift - 在 SwiftUI 类中调用包装的 UIViewController 函数时获取 nil

ios - 如何在表格 View 单元格中使用开关?

ios - 仅当用户快速锁定时,如何运行 func applicationDidEnterBackground 代码?

ios - 如何为 NSObject 中的每个项目添加价格?