2

I am trying to build up an custom table view.

enter image description here

As you can see in the picture, I set the width of the label by default as 160 point in side the story board and change the width on the fly when the table is loaded. I am implementing this by modifying "cellForRowAtIndexPath" delegate method.

So based on the length of the date, I am setting the width of Label1 to maximum utilise the real estate on the phone screen.

    CGFloat timeStampWidth = [cell.timestamp.text sizeWithFont:cell.timestamp.font].width;
    CGFloat ksCompanyNameLableMaxWidth = 235;
    NSLog(@"timeStampWidth:%f", timeStampWidth);
    CGSize companyNameLableSize = CGSizeMake((ksCompanyNameLableMaxWidth - timeStampWidth), cell.companyNameLabel.frame.size.height);
    CGRect newFrame = cell.companyNameLabel.frame;
    newFrame.size = companyNameLableSize;
    cell.companyNameLabel.frame = newFrame;

But when I load the app, all the label1s are set as 160 point as set in the storyboard although by debugging I have seen my code get executed for each cell.

enter image description here

To my surprise, if I scroll down and scroll back up again. The block of code is getting called again and the label is set as I wish.

enter image description here

Moreover, if I switch between the tabs, the label is restored to the abnormal status again.

enter image description here

TypingPanda
  • 1,607
  • 3
  • 19
  • 32
  • 1
    Any auto-resizing/layout rules applied? What sizes are printed to your log? – Wain Nov 06 '13 at 23:02
  • @Wain The size printed out is correct. I think the cell is getting replaced by the cached cell or by some other replacement mechanism. – TypingPanda Nov 06 '13 at 23:36

3 Answers3

3

This sounds like a consequence of auto layout -- when using it, you shouldn't set frames at all, instead you should adjust the constraints. You can make an IBOutlet to the width constraint of your label, and adjust its constant value in code.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Rdelmar, I do use auto layout on one of the other view controller. But I have no constrains at all on this specific table view controller. Dose it matters? – TypingPanda Nov 07 '13 at 00:23
  • @HappyProgrammer, no I don't think it does -- auto layout is turned on for the whole storyboard or xib, not controller by controller. If you don't set any constraints, I think the system automatically adds them for you at run time. – rdelmar Nov 07 '13 at 00:25
  • so I should set the constant dynamically for each cell? when I try to drag out an constant from story board to code (by pressing ctrl and dragging) there is a compiler error says could not connect nslayoutconstraint iboutlet, is there any idea? Thanks – TypingPanda Nov 07 '13 at 00:48
  • Since I am not able to get it by create an iboutlet (which I still have not figure out the reason) I am lazily using this NSLayoutConstraint *labelWidth = [cell.companyNameLabel constraints][0] to get the constraint and modifying its width. Thanks for you advice you are awsome! But I would be pleased if you can tell me why I am not able to create the IBOutlet for this width constraint. FYI, I am using a less than or equal to width constraint on "Label1" – TypingPanda Nov 07 '13 at 00:58
  • @HappyProgrammer, where did you try to connect it to? It needs to be an outlet in your custom cell class. – rdelmar Nov 07 '13 at 00:59
  • @redlmar, I am connecting it inside my tableViewController. I don't have a custom cell class yet. Should I have one? – TypingPanda Nov 07 '13 at 01:01
  • @HappyProgrammer, yes, I think you should, it makes things easier. You probably don't need any code in the .m, just outlets in the .h file. – rdelmar Nov 07 '13 at 01:06
0

If you are using auto layout, you can just set the x and y position, and then nothing for the height and width, and the label will correctly size itself to fit its contents. One caveat if you are using Xibs or Storyboards, don't put any text in your label in the storyboard, or it will want to size to that text. So delete the placeholder text from the storyboard, set the constraints and then you will be good to go.

terry lewis
  • 672
  • 1
  • 5
  • 13
0

You can also create and position the UILabel programatically, this way the iOS auto-layout will not interfere with your frame change.

Ciprian Rarau
  • 3,040
  • 1
  • 30
  • 27