1

I have a UITableView which is displaying update posts from various social networks. I have custom UITableViewCell subclasses, which have a custom UIView inside, which is responsible for drawing all the labels and images, similar to how Apple describes here: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW18

In this view's -layoutSubviews call, I find the size needed for the various labels, and set their frames accordingly. However, after scrolling for a little bit, it's obvious that -layoutSubviews is not being called when a cell is recycled, leading to some cells not having their label frame set big enough to display their content. When passing my data object through the cell to the custom view, I call [self setNeedsDisplay], which I believed would cause the cell to call -layoutSubviews.

How can I best ensure that the labels are properly resized for the content that gets sent to them?

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
s73v3r
  • 1,751
  • 2
  • 22
  • 48

1 Answers1

1

If you want to force layout, send setNeedsLayout (not setNeedsDisplay) to the view. It sounds like you need to send [cell setNeedsLayout] in your tableView:cellForRowAtIndexPath: before returning the cell.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Silly question: Would [cell setNeedsLayout] cause all of the subviews to lay themselves out? – s73v3r Aug 17 '12 at 18:09
  • I think it depends on whether the superview changes the subview's size. I believe a view is only automatically flagged for layout when its size changes, it is added to a window, or it gains or loses subviews. – rob mayoff Aug 17 '12 at 18:12