调整UITableViewCell中标签的位置

时间:2022-08-29 19:04:48

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.

我正在使用UITableViewCell和UITableViewCellStyleSubtitle。但是,由于我用于单元格的背景图像,我不喜欢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属性设置。两个图像都正确显示。

4 个解决方案

#1


35  

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

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

#2


44  

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.

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

#3


1  

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中完成。

#4


-3  

You could probably do this with a CGAffineTransform. The (untested) code to push the detailTextLabel right 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。

#1


35  

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

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

#2


44  

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.

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

#3


1  

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中完成。

#4


-3  

You could probably do this with a CGAffineTransform. The (untested) code to push the detailTextLabel right 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。