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
Share
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 ofUITableViewCellor 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-layoutSubviewsmethod, depending on your exact needs. For more information, see the documentation for -tableView:willDisplayCell:forRowAtIndexPath:.