自动调整 UITableViewCell 内容宽度

发布于 2024-09-11 20:35:01 字数 1284 浏览 5 评论 0原文

我有一些代码可以创建带有滑块的表格单元格。它非常简单,而且在 iPhone 上尺寸也合适。我在这里对其进行了匿名处理:

UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foo"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CGRect contentViewFrame = cell.contentView.frame;
CGRect sliderFrame = CGRectMake(10, 0, 280, contentViewFrame.size.height);
UISlider* slider = [[UISlider alloc] initWithFrame:sliderFrame];
UIImage* minimumImage = [UIImage imageNamed:@"min.png"];
UIImage* maximumImage = [UIImage imageNamed:@"max.png"];
slider.minimumValueImage = minimumImage;
slider.maximumValueImage = maximumImage;
slider.value = 0.5f;
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:slider];
[slider release];

当然,这对于 iPad 来说尺寸不正确。所以我的第一个想法是将 autoresizingMask 属性设置为 UIViewAutoresizingFlexibleWidth。问题解决了,对吧?没有。现在在 iPhone 上,滑块加图像内容的宽度小于 280,因此它不会直接到达末尾 - 它最终会短约 20 像素。

在 iPad 上,同样的情况 - UISlider 的宽度自动调整为距单元格末端约 20 像素。

也许自动调整大小标志正在关注单元格不存在的 accessoryView ?我尝试将其显式设置为 nil ,但我认为默认情况下它是 nil ,所以没有任何改变。

我希望此单元格的内容自动调整为单元格的“完整”宽度,无论设备和方向如何。有没有简单的方法可以做到这一点?

I have some code that creates a table cell with a slider. It's pretty straightforward and it sizes well on the iPhone. I've anonymized it a bit here:

UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foo"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CGRect contentViewFrame = cell.contentView.frame;
CGRect sliderFrame = CGRectMake(10, 0, 280, contentViewFrame.size.height);
UISlider* slider = [[UISlider alloc] initWithFrame:sliderFrame];
UIImage* minimumImage = [UIImage imageNamed:@"min.png"];
UIImage* maximumImage = [UIImage imageNamed:@"max.png"];
slider.minimumValueImage = minimumImage;
slider.maximumValueImage = maximumImage;
slider.value = 0.5f;
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:slider];
[slider release];

Of course, this is incorrectly sized for the iPad. So my first thought was to set the autoresizingMask property to UIViewAutoresizingFlexibleWidth. Problem solved, right? Nope. Now on the iPhone, the width of the slider-plus-images content is less than 280 and so it doesn't go right to the end -- it ends up about 20 pixels short.

On the iPad, the same thing -- the width of the UISlider automatically resizes to about 20 pixels short of the end of the cell.

Perhaps the auto resize flag is paying attention to the non-existent accessoryView of the cell? I tried setting it to nil explicitly, but I think it's nil by default, so nothing changed.

I'd like this cell's content to resize automatically to be the "full" width of the cell, regardless of device and orientation. Is there an easy way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぃ双果 2024-09-18 20:35:01

它的工作原理与您所描述的完全一样。我倾向于认为这是 iOS 的 bug。在 iPAD 上,当您创建新的 UITableViewCell 时,其宽度设置为 320。视图和 contentView 都进行硬编码(!)。如果设置为 UIViewAutoresizingFlexibleWidth,则无法正确调整大小。我将其设置为 view.frame.size.width/2 ,结果很有趣:在 iPhone 上是 160,在 iPad 上是 608!

我最终手动调整了单元格及其内容的大小。

It works exactly how you described. I am inclined to think it's iOS bug. On iPAD when you create new UITableViewCell its width set for 320. hardcoded(!) both view and contentView. It does not resize properly if set to UIViewAutoresizingFlexibleWidth. I had it set to view.frame.size.width/2 with funny results: on iPhone it's 160, on iPad it's 608!!!

I ended up manually resizing my cells and their content.

执妄 2024-09-18 20:35:01

有点晚了,但我今天找到了同一问题的解决方案,但您需要创建一个自定义 UITableViewCell。
然后你可以覆盖该函数

- (void) layoutSubviews
{
    [dateLabel setFrame:CGRectMake(10.f, 16.f, 80.f, 12.f)];
    [textLabel setFrame:CGRectMake(106.f, 16.f, contentView.frame.size.width-105.f + 1.f, 12.f)];
}

在该函数中 self.frame.size.width 是实际的。
它也适用于设备的旋转。

Bit late but i found the solution of the same question today, but you need to create a custom UITableViewCell.
Then you can overwrite the function

- (void) layoutSubviews
{
    [dateLabel setFrame:CGRectMake(10.f, 16.f, 80.f, 12.f)];
    [textLabel setFrame:CGRectMake(106.f, 16.f, contentView.frame.size.width-105.f + 1.f, 12.f)];
}

In that function the self.frame.size.width is the actual one.
And it works with rotation of the device, too.

独留℉清风醉 2024-09-18 20:35:01

您应该能够告诉调整大小系统将对象“粘贴”到距右边缘固定的距离(在该位置调整大小不够远)。如果您尝试使用 IB,您可以创建一个调整宽度大小并固定在右侧的视图。

你也设置了 UIViewAutoresizingFlexibleRightMargin 吗?

You should be able to tell the resizing system to "stick" the object a fixed distance from the right edge (where it's not resizing far enough). If you experiment with IB you can create a view that resizes in width and is fixed to the right side.

Do you have UIViewAutoresizingFlexibleRightMargin set as well?

顾忌 2024-09-18 20:35:01

将单元格的 contentMode 设置为 UIViewContentModeRedraw

Set your cell's contentMode to UIViewContentModeRedraw.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文