0

我有一个UITableViewCell,当滑动以显示删除按钮时,它搞砸了 textLabel,因为我在文本中使用了换行符。

前:

在此处输入图像描述

在删除模式下:

在此处输入图像描述

这很令人沮丧,因为我只想让删除按钮覆盖我的文本,而不是试图让它动画化,以免把它搞砸。

  • 为什么会这样?因为我在字符串中使用了换行符?
  • 我怎样才能防止这种情况发生?
4

2 回答 2

1

为什么会这样?因为我在字符串中使用了换行符?

这是因为您已将标签添加到单元格的 contentView 中(这很好)。当删除按钮出现时,contentView 将自动调整大小,UITableViewCell 的 textLabel 也会调整大小,因为它的 autoresizingMask 设置为UIViewAutoresizingFlexibleWidth。不幸的是,我们无法覆盖默认 textLabel 的 autoresizingMask。

我怎样才能防止这种情况发生?

您必须自己构建一个自定义单元。使用自定义 UILabel 作为属性的子类 UITableViewCell 并将其 autoresizingMask 设置为UIViewAutoresizingNone.

//create and set your frame to whatever you want.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 110, 20)];

//set autoresizing
[label setAutoresizingMask:UIViewAutoresizingNone];

//add to view
[[self contentView] addSubview:label];

//set property to acces later
[self myLabel:label];

//release (if you aren't using ARC)
[myLabel release];

添加一些示例文本以稍后对其进行测试

[[cell myLabel] setText:@"MAX: 72.92 MPH Will fit!"];

如果您使用 interfacebuilder 创建视图/单元格,请确保自动调整大小的蒙版箭头是灰色的

interfacebuilder autoresizingMask

于 2013-01-04T12:38:06.377 回答
0

您应该能够访问 UITableViewCell 的textLabel.Frame属性。子类 UITableViewCell 并将其设置为[UITableViewCell didTransitionToState:(UITableViewCellStateMask)state]

于 2012-08-15T18:49:13.097 回答