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
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;
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
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.
You need to set your cell label frame initially.
cell.textLabel.frame = CGRectMake(0, 0, 40, 10);