1

TableViewCellImage

As screenshot shows I have two labels in UITableViewCell. One label shows the product names and another shows its values. I want their text to align concurrently as you can see it works if the product name is fitting in a single line and fails when it is more than one line. Can I have repeatable labels in a TableViewCell or is there another method to solve my problem. The main idea is that the texts of both labels should align properly.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
SaiPavanParanam
  • 416
  • 6
  • 16
  • The product names i receive are from an array... I am just appending their values into the label text by using "\n" after every element. Same goes for the Values also. – SaiPavanParanam Nov 24 '16 at 07:20
  • Not really much you can do here i think, since it's a single label, you can get the label width and calculate the text size with that width to see the height (can get number of line for text also) and set similar to the right side – Tj3n Nov 24 '16 at 07:30
  • Can you show me an example or link to any tutorial...?? – SaiPavanParanam Nov 24 '16 at 07:31
  • [this](http://stackoverflow.com/questions/15161348/how-can-i-compute-the-number-of-lines-of-a-uilabel-with-a-fixed-width) would be good, just replace `label.text` to your `string` then you can count it – Tj3n Nov 24 '16 at 07:32
  • I did not get your question correctly. Do you want to say you can not set multi-lined text? – Janmenjaya Nov 24 '16 at 07:36
  • @Janmenjaya i can have multi lined text but the texts should align properly ... as you can see in the screenshot that...the value 3591 should be aligned concurrently to ULSD B05 Clear.. – SaiPavanParanam Nov 24 '16 at 07:39
  • I think that nothing can ensure that the value will be aligned with its product if you use only two `UILabel`s. – Luca D'Alberti Nov 24 '16 at 07:58

3 Answers3

0

Usually you should get an array of Product from the server

struct Product {
    let name: String
    let value: UInt
}

Structured in this way, you can generate a small UITableView or you can generate n UIViews inside the cell where each line contains these two UILabel with automatic height. In this way they can be aligned. Receiving a single string from the server is not the best scenario: you can separate the products name by splitting the String for each \n in it and recreate this structure.

Luca D'Alberti
  • 4,749
  • 3
  • 25
  • 45
  • I get the details from the server separately... its just I cannot change the UI here.. I am limited by using only one cell for supply and one cell for delivery... Even i had the idea of having one more cell for products. – SaiPavanParanam Nov 24 '16 at 07:33
  • I'm not saying to create an another cell, but create a `UITableView` (or a group of `UIView`s) inside the `Supply` cell – Luca D'Alberti Nov 24 '16 at 07:34
  • Can you please explain how to do it?? Or any sample code or link to a tutorial – SaiPavanParanam Nov 24 '16 at 07:36
  • You just need to add a `UITableView` to the cell to show a simple cell with one label on the left and one on the right – Luca D'Alberti Nov 24 '16 at 07:37
  • But then i think scrolling would be different right?? It might create an issue – SaiPavanParanam Nov 24 '16 at 07:40
  • The tricky part will be to adapt the `Supply` cell height to contain perfectly the sub-tableview, in this way the inner table view doesn't need to scroll – Luca D'Alberti Nov 24 '16 at 07:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128915/discussion-between-saipavanparanam-and-luca-dalberti). – SaiPavanParanam Nov 24 '16 at 08:01
0

1:Your can create two container views,one for product ,other for value

2:Add two labels into containers,make the labels align.

3:Add more Labels

followed sketch map

enter image description here

Snail
  • 3,046
  • 1
  • 9
  • 12
-1
  • You can give equal height constraint for both the name and its value label. So, whenever the size of name changes it will reflect in the amount label also.
rajtharan-g
  • 432
  • 5
  • 14
  • This doesn't ensure that the `value` value will be on the same line of its product. There can be a misleading of values and products – Luca D'Alberti Nov 24 '16 at 07:57