objective-c 调整 UITableViewCell 中标签的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1303695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:06:32  来源:igfitidea点击:

Adjusting the positions of the labels in a UITableViewCell

iphoneobjective-ccocoa-touchiphone-sdk-3.0uitableview

提问by executor21

I'm using a UITableViewCell with UITableViewCellStyleSubtitle. However, because of the background image I'm using for the cell, I don't like where the detailTextLabel is going. I want to move it and resize it within the cell, but nothing I try seems to work.

我正在使用带有 UITableViewCellStyleSubtitle 的 UITableViewCell。但是,由于我用于单元格的背景图像,我不喜欢 detailTextLabel 的去向。我想移动它并在单元格内调整它的大小,但我尝试的任何方法似乎都不起作用。

CGRect currRect = cell.detailTextLabel.frame;
cell.detailTextLabel.frame = CGRectMake(currRect.origin.x + 20, currRect.origin.y, currRect.size.width - 40, currRect.size.height);

I tried putting this code into both tableView:cellForRowAtIndexPath and tableView:willDisplayCell:forRowAtIndexPath methods, but neither works. The label simply stays where it is, and the same size.

我尝试将此代码放入 tableView:cellForRowAtIndexPath 和 tableView:willDisplayCell:forRowAtIndexPath 方法中,但都不起作用。标签只是保持原样,大小相同。

Adjustment of other properties of the label (textAlignment, backgroundColor, and, of course, the text itself) works correctly.

标签的其他属性(textAlignment、backgroundColor,当然还有文本本身)的调整工作正常。

How to I get the detailTextLabel to move where I need to?

如何让 detailTextLabel 移动到我需要的地方?

In case this matters (though I don't see why it should), this particular cell also has imageView.image and backgroundImage properties set. Both images display correctly.

如果这很重要(虽然我不明白为什么应该这样做),这个特定的单元格还设置了 imageView.image 和 backgroundImage 属性。两个图像都显示正确。

回答by Marius

You could try and adjust the label with the indentation and indentation level:

您可以尝试使用缩进和缩进级别调整标签:

cell.indentationWidth = MY_DESIRED_WIDTH;
cell.indentationLevel = 1;

(for unindented cells)

(对于未缩进的单元格)

Or for indented cells just do:

或者对于缩进的单元格,只需执行以下操作:

cell.indentationLevel = cell.indentationLevel + 1;

Both these should shift/indent your labels.

这两个都应该移动/缩进你的标签。

回答by Andrew Pouliot

You'll want a custom UITableViewCellsubclass that overrides layoutSubviews to do the dirty work of laying out whatever subviews are in there. Calling super then adjusting the detailTextLabel.frameis probably the easiest thing to do.

您将需要一个UITableViewCell覆盖 layoutSubviews的自定义子类来完成布置任何子视图的肮脏工作。调用 super 然后调整detailTextLabel.frame可能是最简单的事情。

回答by Jordan

Or, you can change the cell from UITableViewCellStyleSubtitle, to the default and add your own objects to cell.contentView and place them where you want. You don't necessarily need to subclass UITableViewCell (e.g. custom cell) or use layoutSubViews. It can all be done in tableView:cellForRowAtIndexPath.

或者,您可以将单元格从 UITableViewCellStyleSubtitle 更改为默认值,并将您自己的对象添加到 cell.contentView 并将它们放置在您想要的位置。您不一定需要继承 UITableViewCell(例如自定义单元格)或使用 layoutSubViews。这一切都可以在 tableView:cellForRowAtIndexPath 中完成。

回答by John Franklin

You could probably do this with a CGAffineTransform. The (untested) code to push the detailTextLabelright 10 pixels would look roughly like:

您可能可以使用CGAffineTransform. 推送detailTextLabel正确 10 个像素的(未经测试的)代码大致如下所示:

CGAffineTransform translate = CGAffineTransformMakeTranslation(10.0, 0.0); 
[detailTextLabel setTransform:translate];

While you can do this, I think you'll be happier creating your own custom UITableViewCell.

虽然您可以这样做,但我认为您会更高兴创建自己的自定义UITableViewCell.