[英]How to keep the text of cell label at top when cell's height increases?
I am increasing cell's height in the method heightForRowAtIndexPath
, as a result of it, the label
is coming centered. 我在heightForRowAtIndexPath
方法中增加单元格的高度,结果, label
居中。 How do make sure that label
is still at top with the text irrespective of the height of the row? 无论行高如何,如何确保label
仍在文本顶部?
While allocating UITableViewCell, you need to do like this.. 在分配UITableViewCell时,您需要这样做。
UITableViewCell * cell;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:string];
At the time of initialization with style: UITableViewCellStyleSubtitle
will come. 在样式初始化时: UITableViewCellStyleSubtitle
将会出现。
Hope this will work.. 希望这会工作..
Write Following Code: 编写以下代码:
UILabel *myLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, height)];
[cell.contentView addSubView: myLable];
If Also You want myLable
fram is similare to myLable.text
then write.. 如果还希望myLable
框架myLable.text
则编写。
[myLable sizeToFit];
You can do it in two ways. 您可以通过两种方式来实现。
Create a custom label and set it's frame. 创建一个自定义标签并设置其框架。
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)]; [cell.contentView addSubView:lbl];
Use a UITableViewCell
subclass and override -layoutSubviews
. 使用UITableViewCell
子类并重写-layoutSubviews
。 In this method, you'll want to call [super layoutSubviews]
. 在此方法中,您将要调用[super layoutSubviews]
。
- (void)layoutSubviews { [super layoutSubviews]; CGSize size = self.bounds.size; CGRect frame = CGRectMake(0.0f, 0.0f, size.width, size.height); self.textLabel.frame = frame; self.textLabel.contentMode = UIViewContentModeScaleAspectFit; }
You cannot change the position of textlabel
as it is auto-adjusted. 您无法更改textlabel
的位置,因为它是自动调整的。
First option is you need to subclass UITableViewCell and customize textLabel frame. 第一个选择是您需要继承UITableViewCell并自定义textLabel框架。
Another is that you create your own custom label and make textlabel nil. 另一个是您创建自己的自定义标签,并使textlabel为nil。 So whenever cell's height will change, your label position will not change. 因此,只要单元的高度发生变化,您的标签位置就不会改变。
设置UILabel
的autolayout
Top属性。
try this: 尝试这个:
set False to "Autoresize Subviews" property of your custom cell... 将False设置为自定义单元格的“ Autoresize Subviews”属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.