dclowd9901
dclowd9901

Reputation: 6836

UILabel shifting X position on TableView update

In my tableview, I replace my default UILabel with a different UIView on tap, and then replace it back with an archived version of the UILabel when the picker is ready to be dismissed. The UILabel does change contents in the process, as well.

The result is that the UILabel is seen to shift right when the cell resizes. You can see the result here in video:

http://youtu.be/n8Sg5qr_aAo

I've tried messing with the constraints, and if I explicitly set the frame to be in the right place, it'll work, but only after the first time. If it helps, it also seems to place itself so that the text is centered in the middle of where the UILabel was placed (as though the text was centered in a larger UILabel), but upon inspection, it's the frame.origin.x of the UILabel that's at fault.

Any thoughts? I feel like this has something to do with the constraints system, but nothing I did with them seemed to have any impact.

Upvotes: 0

Views: 108

Answers (1)

Mika
Mika

Reputation: 5845

I find that the best way to implement this is two use two cells. One has the label the other the picker. I keep both visible while the picker is open which is how apple does it in the calendar app. If you want to do it all one cell why don't you play with the height of the cell and the alpha of the label so you don't have to do any replacing?

  1. In your cell you add both the label and the picker
  2. On load: you set the height of the cell to the height of the label + margins so the picker is invisible
  3. On tap: you change the height of the cell the cell and hide the label so now only the picker is visible
  4. On done: you set the value of the label, resize the cell hiding the picker, and you unhide the cell

Upvotes: 1

Related Questions