0

In iOS6, I cannot seem to get the width of the cell in cellForRowAtIndexPath for the grouped table style. Logging either the frame or the bounds for either the cell or its contentview returns 320 - even on iPad. I need to determine the cell width programmatically for any device as I need to calculate text sizes. Any advice in getting the correct cell width for a grouped tableview in cellForRowAtIndexpath would be appreciated please

RunLoop
  • 20,288
  • 21
  • 96
  • 151
  • Judt checking: have you tred logging the frame of cell.contentView? – Ravi Oct 13 '12 at 06:46
  • 1
    Since you tagged this ios6, why not just use constraints and let the autolayout system calculate it for you? – Jason Coco Oct 13 '12 at 06:46
  • @Jason - The app needs backwards compatibility to 4.3 – RunLoop Oct 13 '12 at 06:48
  • 1
    Please don't tag with iOS 6 then, please make it clear you're targeting iOS 4.3 in the question and tag things like ios, cocoa-touch, etc. It will make it much more likely you'll get the answers you need. – Jason Coco Oct 13 '12 at 06:50
  • @Jason It was tagged iOS6 because it is an iOS6 issue. – RunLoop Oct 13 '12 at 06:52
  • see http://stackoverflow.com/questions/8541184/getting-width-of-a-cell-in-a-grouped-uitableview-for-setting-section-header-widt read the last answer. when you log the frame.width of cell, it always return the width of table view – rakeshNS Oct 13 '12 at 06:58
  • It's actually not an iOS 6 issue, you're just noticing it on iOS 6 because certain rendering mechanics have changed to support autolayout, but I see your point. My point is just that tagged for iOS 6 with nothing in the question about supporting previous versions is more likely to get you responses suggesting you use the iOS 6 convenience tools. – Jason Coco Oct 13 '12 at 07:00

1 Answers1

2

The method you're using is the wrong place to calculate any kind of view-related constraints. The -tableView:cellForRowAtIndexPath: method is part of the table view's data source, not its delegate. You cannot rely on the frame or anything else here to be meaningful, it's meant as the place to configure the cell's /data/.

If you need to make calculations to view frames and such, and you're not using a custom subclass of UITableViewCell (i.e., you're just adding views to a default instance of UITableViewCell or configuring stock views), you would set up any frame-related / view specific attributes in the /delegate/ callback -tableView:willDisplayCell:forRowAtIndexPath: method. This is the place to configure any of the visible/view-related properties of your cell, and you will now have accurate layout information for the cell (its bounds will be correct, any layout/configuration of internal views will be complete, etc.).

If you have a custom subclass already, you can either do your view related property configuration in the delegate callback above, or you can do it in UIView's -layoutSubviews method, depending on your exact needs. For more information, see the documentation for -tableView:willDisplayCell:forRowAtIndexPath:.

Jason Coco
  • 77,985
  • 20
  • 184
  • 180