Search code examples
swifttableviewcelltablelayoutswift4.1

Swift - Custom cell width and height for textlabel and detailTextLabel


I wanna manage the long text in textlabel and detailtextlabel for the cells in UITableview because if the textlabel has a long text the detailedtextlabel truncate with "..." and viceversa.

Here's the problem:

enter image description here

So I wanna know if there's a way to manage the space between textlabel and detailtextlabel so there's no truncate and give them more lines. I tried with:

...
if tableData[indexPath.row].clave.count > 6 {
     cell?.detailTextLabel?.numberOfLines = 5
     cell?.textLabel?.numberOfLines = 5
} else {
     cell?.detailTextLabel?.numberOfLines = 1
}

cell?.textLabel?.text = nueva_cadena_string
cell?.detailTextLabel?.text = tableData[indexPath.row].clave
cell?.textLabel?.textColor = UIColor.brown
cell?.textLabel?.font = UIFont.boldSystemFont(ofSize: 15.0)
cell?.textLabel?.lineBreakMode = .byWordWrapping
return cell!;

But it doesn't work very well, it seems to give them just 2 lines, not 5 and I can't change the width for the textlabel and detailtextlabel.


Solution

  • Try making your cell custom as this:

    First add a tableViewCell in storyboard and 2 labels

    next try doing thisfinally select your custom CocoaTouch class