22

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.

executor21
  • 4,532
  • 6
  • 28
  • 38

4 Answers4

43

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.

Marius
  • 3,372
  • 1
  • 30
  • 36
  • 1
    This works great as it means that for some layouts (where you have an image to the left) you don't need to add your own label to replace the existing one. Thanks. – Tony Day Aug 31 '11 at 09:45
  • Grear answer, good to finally know something like this exists! – pmk Jan 30 '13 at 09:32
  • 5
    It's worth noting that `cell.indentationWidth` can be a negative number - which means you don't need to subclass the cell if you just need to shift the entire contentView left. – SaltyNuts Jan 31 '13 at 18:53
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.

Andrew Pouliot
  • 5,423
  • 1
  • 30
  • 34
  • Thank you. I knew you could do this already, but I didn't know it was the layoutSubviews method that had to be overridden. – executor21 Aug 20 '09 at 17:24
  • This is great advice. However, I have this problem still where, while I can customize the label dimensions, for instance, I cannot get it to wordwrap and show multiple lines, i.e.: self.textLabel.numberOfLines = 2; self.textLabel.lineBreakMode = UILineBreakModeWordWrap; – Joey Oct 03 '10 at 03:42
  • Behavior of the .textLabel property should be identical to any other UILabel, so perhaps you're not giving it enough vertical space? – Andrew Pouliot Apr 21 '12 at 03:20
  • This worked for me but I was also moving my content outside of my cell which was then being clipped by the cell above - led me to this question where I have added my eventual solution: http://stackoverflow.com/questions/2842964/adding-a-subview-larger-than-cellheight-to-a-uitableviewcell/19928425#19928425 – Sam Nov 12 '13 at 12:55
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.

Jordan
  • 21,746
  • 10
  • 51
  • 63
  • Tried this. The main label then moves into the center of the cell, and it can't be moved, either. Subclassing seems less troublesome. – executor21 Aug 20 '09 at 17:26
-3

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.

John Franklin
  • 4,962
  • 1
  • 23
  • 27
  • Didn't work. Apparently, you can't adjust the positions of subviews in any way without subclassing the cell. – executor21 Aug 20 '09 at 17:25