Ethan Zhao
Ethan Zhao

Reputation: 249

How to get label in cells in table to go on to next line instead of getting cut off screen? (xcode 8)

So basically I am loading text from an array into cells in a table view, but instead of going on to the next line like I want, the inputted text labels get cut off the screen in the single cell. I looked online and it said to set numberOfLines to 0 and lineBreakMode to NSLineBreakingByWordWrapping, but it isn't working. What am I doing wrong? Thanks!

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "homeSeries", for: indexPath)

    // Configure the cell...

    cell.textLabel?.text = dataArrayRelease[indexPath.row] + " " + dataArraySeries[indexPath.row] + " by " + dataArrayGroup[indexPath.row]
    cell.textLabel?.numberOfLines = 0
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping

    return cell
}

Upvotes: 2

Views: 389

Answers (1)

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

Your cellForRowAt method code is proper. you need to do some stuff related row height. Set up your tableview row height as UITableViewAutomaticDimension in viewDidLoad() method.

yourTableview.rowHeight = UITableViewAutomaticDimension
yourTableview.estimatedRowHeight = 44

OR

you can add Bellow UITableViewDelegate method in your Controller

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

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

Upvotes: 2

Related Questions