简体   繁体   English

更改表格视图单元格中的标签框架

[英]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. 拜托,有人帮我。

If I want my textLabel indented by 16, then in cellForRowAtIndexPath: , I would type: 如果我想让textLabel缩进16,则在cellForRowAtIndexPath: ,我将输入:

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 . 单元格属性indentationLevel (一个CGFloat)确定缩进textLabel的宽度indentationWidth (一个NSInteger)的textLabel

The default value of indentationWidth is 10.0 and the default value of indentationLevel is 0. 的默认值indentationWidth是10.0和的默认值indentationLevel是0。

这是一篇关于女巫的文章,我描述了如何创建自定义tableViewCell可能对它与图像和标签的自定义tableViewCell示例有所帮助。

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. 首先,您需要通过以编程方式向其中添加UILabel对象或通过Interface Builder设计它来自定义UITableViewCell实例。

Once you have placed the label, to change its position you should consider editing its frame property. 放置标签后,要更改其位置,应考虑编辑其frame属性。 Eg: 例如:

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. 让我知道是否有帮助。

What you want to do is to create a subclass of UITableViewCell , and override the layoutSubviews methods. 您要做的是创建UITableViewCell的子类,并覆盖layoutSubviews方法。 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. 您所有的自定义内容都应添加到表格视图单元格的contentView子视图的子视图中,而不是直接作为表格视图单元格的子视图。 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. 您也可以直接制作UIView的子类,并用作完全覆盖contentView的子视图,并在那里进行layoutSubviews的覆盖,而无需调用super实现。

thanx everyone it's done. 谢谢大家。 I just set the frame for proper position and add to the contentView of tableviewcell. 我只是将框架设置为正确的位置并添加到tableviewcell的contentView中。

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. 这里slideCell1是tableviewcell的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM