简体   繁体   English

当单元格的高度增加时,如何保持单元格标签的文本顶部?

[英]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. 您可以通过两种方式来实现。

  1. Create a custom label and set it's frame. 创建一个自定义标签并设置其框架。

     UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)]; [cell.contentView addSubView:lbl]; 
  2. 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. 因此,只要单元的高度发生变化,您的标签位置就不会改变。

设置UILabelautolayout Top属性。

try this: 尝试这个:

set False to "Autoresize Subviews" property of your custom cell... 将False设置为自定义单元格的“ Autoresize Subviews”属性。

暂无
暂无

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

相关问题 标签文本增加时,如何以编程方式增加集合视图单元格的高度? - How to programmatically increase collection view cell height when label text increases? 当其中的 label 文本被修改时,有没有办法改变 tableView 单元格的高度? - Is there a way to change tableView cell's height, when the label text in it is modified? 设置UITableViewCell高度和基于文本高度的单元格文本标签 - Setting UITableViewCell height and the cell's text label based on the text height 在运行时设置标签文本多行时如何在tableview单元格内设置单元格的高度 - How to set height of cell inside tableview cell when set label text multiline at run time 如何根据文本标签长度动态设置表格单元格高度? - how to set table cell height dynamically depending on text label length? 使用自动布局表格视图时,如何增加表格视图单元格中的标签高度(取决于文本)? - How to increase Label Height(Depend on Text) in Tableview cell when used Autolayout Table view? 滑动以显示Tableview单元格中的编辑操作时,如何保持文本标签的可见性? - How to keep text label in view when swiping to reveal edit actions in tableview cell? 如何在 CollectionView 中将单元格高度调整为 Label 高度? - How to resize cell height to Label height in CollectionView? 根据标签文字和图片计算像元高度 - Calculate Cell height on basis of label text + image 更改单元格高度后单元格文本标签未居中 - cell text label not centered after changing the cell height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM