我在 View Controller 中有一个 TableView ,在单元格中,当文本太长时,我的文本会被截断。如何让单元格根据单元格中的内容自动更改或让文本换行以免文本被截断?这是一个 image我在说什么。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func allowMultipleLines(tableViewCell: UITableViewCell) {
tableViewCell.textLabel?.numberOfLines = 0
tableViewCell.textLabel?.lineBreakMode = .byWordWrapping
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return courses.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let course:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "course")!
course.textLabel?.text = courses[indexPath.row]
return course
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "Courses", sender: nil)
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailView: DetailCoursesViewController = segue.destination as! DetailCoursesViewController
selectedRow = (table.indexPathForSelectedRow?.row)!
detailView.setCourseTitle (t: courses[selectedRow])
detailView.setCourseDescription (d: courseDescription[selectedRow])
detailView.setCredits (c: credits[selectedRow])
detailView.setPrerequisites (p: prereq[selectedRow])
}
这是 image代码的
最佳答案
首先,您需要为 TableViewcell 中的 contentView 设置标签的前导、尾随、底部和顶部约束。
override func viewDidLoad() {
super.viewDidLoad()
self.tabelView.estimatedRowHeight = 50
self.tabelView.rowHeight = UITableViewAutomaticDimension
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return courses.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let course:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "course")!
cell.textLabel.numberOfLines = 0
course.textLabel?.text = courses[indexPath.row]
return course
}
关于ios - 根据单元格中的内容自动更改单元格内容 - Xcode 9.2 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49291374/