开发者

Change frame of label in table view cell

Yesterday I asked how to change the position of a label in a table view cell, but I still don't get it. Actually, I have a label in the right corner of the cell. When I change to landscape mode,开发者_高级运维 it moves to the center, but I want it to be in the right corner. Please, someone help me.


Here is Article In witch I describe how to create custom tableViewCell may be it helps Custom tableViewCell example with images and lables Check it.


What you want to do is to create a subclass of UITableViewCell, and override the layoutSubviews methods. It will be called when the size of the cell changes, for example when you change interface orientation.

In this method you call the super implementation first to properly update layout for content, accessory, backgrounds, etc. views. And then update your custom contents.

All your custom contents should be added subviews of the contentView subview of the table view cell, never as direct subviews to the table view cell. This is important, otherwise selection and highlight will screw up.

You could also make a subclass of UIView directly and use as a subview completely covering contentView and do the override of layoutSubviews there, without the need for calling the super implementation.


First of all you need to customize you UITableViewCell instance, either by programmatically adding a UILabel object to it, or designing it through Interface Builder.

Once you have placed the label, to change its position you should consider editing its frame property. E.g.:

UILabel *label = cell.label; // You should change this code to properly read the label
CGRect frame = label.frame;
frame.position.x = new_x_position;
frame.position.y = new_y_position;
label.frame = frame;

This way you can update the position of the label according to your needs.

If the problem you are facing is that of the correct positioning of the label while changing interface orientation, you should properly set the autoresizing mask for that component. To make it right aligned:

label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

To make it left aligned:

label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

Also, you can choose to auto adapt the width of the label:

label.autoresizingMask = UIViewAutoresizingFlexibleWidth;

Or, you can choose a combination of the previous ones:

label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
                         UIViewAutoresizingFlexibleRightMargin |
                         UIViewAutoresizingFlexibleWidth;

Let me know if this helps.


thanx everyone it's done. I just set the frame for proper position and add to the contentView of tableviewcell.

lideCell1.sliderValueForSecond.frame=CGRectMake(25, 8, 400,23);
                slideCell1.tickLbl.frame=CGRectMake(430, -3, 27,40);
                slideCell1.tickLbl.text=@"+";
                slideCell1.tickLbl.font = [UIFont boldSystemFontOfSize:36.0];
                [slideCell1.contentView addSubview:slideCell1.sliderValueForSecond];
                [slideCell1.contentView addSubview:slideCell1.tickLbl];

Here slideCell1 is object of tableviewcell.


If I want my textLabel indented by 16, then in cellForRowAtIndexPath:, I would type:

cell.indentationWidth = 8.0;
cell.indentationLevel = 1;

The cell property indentationLevel (a CGFloat) determines how many multiples of width indentationWidth (an NSInteger) to indent the textLabel.

The default value of indentationWidth is 10.0 and the default value of indentationLevel is 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜