-1

我在方法中增加了单元格的高度,heightForRowAtIndexPath结果label是居中。label无论行高如何,如何确保它仍然在文本的顶部?

4

6 回答 6

2

在分配 UITableViewCell 时,你需要这样做..

UITableViewCell * cell;

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:string];

在用 style: 初始化的时候UITableViewCellStyleSubtitle会来。

希望这会奏效..

于 2013-06-26T06:35:31.913 回答
1

编写以下代码:

UILabel *myLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, height)];
[cell.contentView addSubView: myLable];

If Also You want myLablefram 类似于myLable.textthen write..

[myLable sizeToFit];
于 2013-06-26T05:58:57.057 回答
1

你可以通过两种方式做到这一点。

  1. 创建一个自定义标签并设置它的框架。

    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [cell.contentView addSubView:lbl];
    
  2. 使用UITableViewCell子类并覆盖-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;
     }
    
于 2013-06-26T06:05:48.900 回答
1

您无法更改 的位置,textlabel因为它是自动调整的。

第一个选项是您需要继承 UITableViewCell 并自定义 textLabel 框架。

另一个是您创建自己的自定义标签并使 textlabel 为零。因此,只要单元格的高度发生变化,您的标签位置就不会改变。

于 2013-06-26T06:10:50.837 回答
0

设置 的autolayoutTop 属性UILabel

于 2013-06-26T05:57:22.480 回答
0

尝试这个:

将 False 设置为自定义单元格的“自动调整子视图”属性...

于 2013-06-26T06:19:00.477 回答