UILabel in UITableView setting size

I would like to add a label subview to a UITableViewCell. I can easily set the frame origin and size myself to specific values but how can I use the current/default cell label height and position?

  UILabel *labPerHour;

  cell.textLabel.text       = @"Rate";

  labPerHour = [[UILabel alloc] init];

  //labPerHour.frame = CGRectMake( 280, cell.textLabel.frame.origin.y, 80, cell.textLabel.frame.size.height );
  labPerHour.frame = CGRectMake( 260, 7, 30, 30 );
  labPerHour.text  = @"$/hr";

  [cell addSubview:labPerHour];

I would like the commented-out line to work.. or something similar.

Can somebody please help? This code is of course part of my cellForRowAtIndexPath method.

preguntado el 31 de julio de 12 a las 10:07

Have you created prototype cell using ios5 storyboard feature? -

No, I'm still using Xcode 4. Thanks. -

4 Respuestas

I am not sure. May be it will work. I haven't tried this before!!

CGRect theFrame = cell.textLabel.frame;
theFrame.origin.x = 280;
theFrame.size.width = 80;
labPerHour.frame = theFrame;

Respondido 31 Jul 12, 10:07

Well that would and also (potentially) change the size of cell.textLabel to the specific sizes you've quoted above.. What I am after is the default cell.textLabel sizes - whatever iOS assigns to them initially. - SparkyNZ

What was the output you got for this?? It is similar to what you had tried in your code!! labPerHour.frame = CGRectMake( 280, cell.textLabel.frame.origin.y, 80, cell.textLabel.frame.size.height ); Isn't it?? - Nina

If you trying to get the frame of default label of uitable view, then you may try this

CGRect myframe;
myframe= cell.textlabel.frame;

now use this as per need

Respondido 31 Jul 12, 10:07

You need to provide coordinates relative to the superview’s coordinate system. These coordinates need to make sense, so your label is still positioned inside your cell's frame, not outside it.

Because every view and window defines its own local coordinate system, you need to be aware of which coordinate system is in effect at any given time. Every time you draw into a view or change its geometry, you do so relative to some coordinate system. In the case of drawing, you specify coordinates relative to the view’s own coordinate system. In the case of geometry changes, you specify coordinates relative to the superview’s coordinate system. The UIWindow and UIView classes both include methods to help you convert from one coordinate system to another.

Section "View Geometry and Coordinate Systems" of Ver la guía de programación para iOS.

Respondido 31 Jul 12, 10:07

You need to set your cell label frame initially.

cell.textLabel.frame = CGRectMake(0, 0, 40, 10);

Respondido 31 Jul 12, 12:07

But surely it has a frame already? My cell and text label already have a default size which I don't want to change - I just want to make use of that default size so my additional subview labels are the same size as the default label in the cell. If I had my Mac with me I'd print out the contents of cell.textLabel.frame but will have to wait until I can get back to my Mac.. - SparkyNZ

ya its already have frame...and i tried to get frame according to your requirement , but i was getting cell textlabel frame size after scrolling the table...means in starting , when table load..i was getting cell text label height = 0, and after first scrolliung i was getting actual textlabel size...; - Rajneesh071

so you have to scroll the table first to get your textLabel size or u have to provide textLabel frame size initially.. - Rajneesh071

I understand what you are saying. The other answers would suggest that the normal way of doing this is to specify an actual size first.. I honestly would have thought that there would have been a simpler way of determining/predicting what iOS is going to use.. but perhaps there is not. - SparkyNZ

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.