1

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.

kelin
  • 11,323
  • 6
  • 67
  • 104
Daniel Campos
  • 55
  • 1
  • 14

3 Answers3

0

Try setting the tableviews row dimension to automatic

 yourTableView.rowHeight = UITableViewAutomaticDimension
 yourTableView.estimatedRowHeight = 80.0

try setting both textlabels number of lines either in the storyboard or in code to 0 as then they will all have the amount of lines they need.

AD Progress
  • 4,190
  • 1
  • 14
  • 33
  • I put them and it looks the same, I need some way to break the long text at half or short the textlabel width because it tooks almost all the cell width – Daniel Campos Jun 08 '18 at 18:00
  • Try the above. setting the line numbers to 0 on the textlabels plus the code I mentioned. See if that helps – AD Progress Jun 08 '18 at 18:33
  • I change the text size and change the line number to 0 and add the two lines for your answer. It looks good in most of the cases, but I've problems with some lines where the textlabel is very long but not enough to break in two lines and I can't see the detailtextview in that cases. – Daniel Campos Jun 08 '18 at 18:36
  • Ok I made some tricks with the string and break them programmatically, now it looks awesome, thanks. – Daniel Campos Jun 08 '18 at 18:52
0

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

AD Progress
  • 4,190
  • 1
  • 14
  • 33
  • It give me these errors in the IBOutlets: Cannot override strong property with weak property, Getter for 'textLabel' with Objective-C selector 'textLabel' conflicts with getter for 'textLabel' from superclass 'UITableViewCell' with the same Objective-C selector, Property 'textLabel' with type 'UILabel!' cannot override a property with type 'UILabel?' – Daniel Campos Jun 08 '18 at 20:06
  • Okl, my mistake, I needed to change the names, they can't be the same as the default ones. – Daniel Campos Jun 08 '18 at 20:10
  • Ok, I did all what you specify, now what? I can't call the cell?.firstTextLabel (firstTextLabel is the name of my IBOutlet for the textLabel) – Daniel Campos Jun 08 '18 at 20:14
  • Ok, I check here: https://www.ralfebert.de/ios-examples/uikit/uitableviewcontroller/custom-cells/ and it looks 100% awesome now, thank you so much. – Daniel Campos Jun 08 '18 at 20:43
  • I am happy that I could help. Good luck with the app. – AD Progress Jun 09 '18 at 10:38
0

You can subclass UITableViewCell and override the layoutSubviews() method. You will need to change the layout of textLabel and detailTextLabel there. Remember that these elements lie inside contentView, not in the cell itself.

But better you add two custom labels on the Storyboard and configure them as you want. It's easier.

kelin
  • 11,323
  • 6
  • 67
  • 104