2

In my UITableViewCell of UITableViewCellStyleSubtitle I want to fill the complete maximum width of detailTextLabel; one short text on the left (left-aligned) and another short text at the far right (right-aligned). The two texts will leave enough space between them.

I found this but that does not seem to work; it simply adds a fixed space between the left and right part.

If the detailTextLabel had a fixed width spreading the full available cell space, it wouldn't be that hard: The width of both strings could be calculated and the remaining space filled using NSKernAttributeName for example. But, unfortunately detailTextLabel is dynamically adjusted for the current content.

Has anyone figured out an elegant solution to do this? (Other than in small steps making the text longer and longer until it hits the full potential width of detailTextLabel and snaps back a little because of the ....)

Community
  • 1
  • 1
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
  • 2
    Can you not subclass your cell and split detailTextLabel into two? You can then assign any width you want and also any space between them that you want. You can also embed them in the UIView so that their combined width is always the same – Andriy Gordiychuk Aug 19 '15 at 21:47
  • @AndriyGordiychuk I thought of subclassing, but because I want to have standard iOS looks I'd have to manually copy the full layout including font and colors. Other method would be to add a `UILabel` subview to the cell, but then again I'd have to copy. I'm hoping there's a simpler way. – meaning-matters Aug 19 '15 at 21:52

1 Answers1

5

This worked for me. Try this:

 cell.detailTextLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping;
 cell.detailTextLabel!.numberOfLines = 0;

If you want to set the left view and right view in the tableView.

Select your tableView and go to attribute inspector and set the prototype cell to 1 as shown in the screenshot below:

enter image description here

Select your tableView cell and go the attributes inspector and do the settings as shown in the screenshot below:

enter image description here

After selecting the right detail the tableview cell will consist as following screenshot:

enter image description here

The text set in "cell.textLabel!.text" is the left view (Title) and the text set in the "cell.detailTextLabel!.text" is the right view (Detail)

Hope this might be helpful.

Karlos
  • 1,653
  • 18
  • 36
  • I have updated my answer for left and right view in the tableView. – Karlos Aug 20 '15 at 08:00
  • Thanks for the effort, but unfortunately does not work. Three reasons: 1. I'm already using the `textLabel` 2. I'm not using storyboard nor XIB 3. As I said, I don't want custom cell but `UITableViewCellStyleSubtitle` instead. – meaning-matters Aug 20 '15 at 08:17
  • 1
    I've +1 two of your other answers to compensate for your kind effort. – meaning-matters Aug 20 '15 at 08:18