Adjusting the positions of the labels in a UITableViewCell

久未见 提交于 2019-11-30 13:00:26

问题


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.

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.

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

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

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.


回答1:


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.




回答2:


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:


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.




回答4:


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

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.



来源:https://stackoverflow.com/questions/1303695/adjusting-the-positions-of-the-labels-in-a-uitableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!