10

I have a UITableViewCell that I would like to add a view to the right (in addition to the accessory view). I tried setting the size of textLabel to be a few pixels narrower but it just resizes it back.

Is there any way to resize textLabel?

4 Answers 4

17

Actually it CAN be resized if you create an UITableViewCell subclass and override the layoutSubviews method:

- (void)layoutSubviews {
[super layoutSubviews];  //The default implementation of the layoutSubviews

CGRect textLabelFrame = self.textLabel.frame;
textLabelFrame.size.width = _textLabelMaxWidth;
self.textLabel.frame = textLabelFrame;
}

Hope it helps.

3
  • This only works for me when the cell is reloaded, any ideas? Mar 16, 2013 at 22:09
  • @AdamCarter Yes, that is how is suppose to work, unless you want to change it at a different time and from a different event. Mar 26, 2013 at 10:22
  • 1
    This was very useful for me. I did have a subclass of UITableViewCell and the label sizes would keep returning to the storyboard defaults. I appreciate the advice with this!
    – mikeho
    May 17, 2014 at 2:52
8

The object referenced by the default textLabel property, in UITableViewCell instances of type UITableViewCellStyleDefault, cannot be resized, at least not in my experience. The best idea in these cases is to create your own subclass of UITableViewCell, in code or even with Interface Builder if you want, and give it the layout that you want.

1

I've noticed that the textLabel resizes to accomodate the accessoryView. So if you want the label to use the entire cell, you could set accessoryView to nil (I think this is the default behavior anyway). Or, for example, if you want the label to take only the left half the cell, make accessoryView be an empty UIView that spans the right half the cell. (Doesn't have to be empty, you can put stuff in there...my point is that it WILL shrink the textLabel.)

0

You can do resizing UITableView Cell in this simplest way.First,take a look at my sample project.

ResizableUIView

It will show you how you can resize uitableview cell base on its inner component in simplest way by creating constraints.

    tblTest.estimatedRowHeight = 100 // This is where you can set your estimated row height
    tblTest.rowHeight = UITableViewAutomaticDimension

Another important fact is your label lines : enter image description here

it should be 0

In order to make you understand,I set UILabel inside UIView,take a look at how I created constraints on its parent UIView.

If you still need any help,just ask.

Good Luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.