Reputation: 31
I have a table View in a View Controller, and within the cells, my text gets cut off when it's too long. How do I get the cell to automatically change based on the content in the cell or get the text to wrap so the text doesn't get cut off? Here's an image of what I'm talking about.
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])
}
Here is the image of the code
Upvotes: 0
Views: 80
Reputation: 412
First you need to set leading, trailing, bottom and top constraints of label to contentView in the TableViewcell.
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
}
Upvotes: 1
Reputation: 182
1- Give a bottom constraint between the label and cell bottom like 10
2- In the function tableView(_:cellForRowAt:)
do the following:
cell.textLabel.numberOfLines = 0
cell.textLabel.lineBreakMode = .byWordWrapping
Upvotes: 0
Reputation: 2368
1) Set UILabel property numberOfLines = 0
inside cellForRowAt
of UITableView
2) Inside ViewDidLoad
write below code
self.tabelView.estimatedRowHeight = 44
self.tabelView.rowHeight = UITableViewAutomaticDimension
Upvotes: 1